import heapq area = [] shark = (0,0) sharkSize = 2 moveTime = 0 eatCount = 0 N = int(input()) for i in range(N) : area.append(list(map(int, input().split()))) for j in range(N) : if area[i][j] == 9 : shark = (i,j) area[i][j] = 0 while True : visited = [[False]*N for _ in range(N)] visited[shark[0]][shark[1]] = True pq = [(0, shark[0], shark[1])] while pq : ct, cy, cx = heapq.heappop(pq) if area[cy][cx] != 0 and area[cy][cx] < sharkSize : area[cy][cx] = 0 eatCount += 1 moveTime += ct shark = (cy, cx) if eatCount == sharkSize : eatCount = 0 sharkSize += 1 break for nx, ny in [(cx,cy-1), (cx-1,cy), (cx+1,cy), (cx,cy+1)] : if 0<=nx