import heapq H,W=map(int,input().split()) A=[] for _ in range(H): A.append(list(map(int,input().split()))) hq=[[] for _ in range(2)] heapq.heappush(hq[0],(A[0][0],0,0)) heapq.heappush(hq[1],(A[-1][-1],H-1,W-1)) use=[[-1]*W for _ in range(H)] d=[[1,0],[-1,0],[0,1],[0,-1]] flag=[[[False]*W for _ in range(H)] for _ in range(2)] count=0 while True: p,x,y=heapq.heappop(hq[count%2]) if use[x][y]!=-1: continue use[x][y]=count%2 for x_d,y_d in d: x_m=x+x_d y_m=y+y_d if 0<=x_m