import heapq D=[[1,0],[0,1],[-1,0],[0,-1]] H,W=map(int,input().split()) A=[] for i in range(H): A.append([int(_) for _ in input().split()]) C=[[None for j in range(W)] for i in range(H)] Q=[[(A[H-1][W-1],(H-1,W-1))],[(A[0][0],(0,0))]] heapq.heapify(Q[1]) heapq.heapify(Q[0]) step = -2 while True: step += 1 k = step%2 while True: _, p = heapq.heappop(Q[k]) Y, X = p if C[Y][X] is None: break C[Y][X]=k f = False for d in D: y = Y + d[0] x = X + d[1] if 0<=y