import heapq H,W=map(int,input().split()) A=[list(map(int,input().split())) for _ in range(H)] v=[] for i in range(H): for j in range(H): v.append((A[i],i,j)) v.sort() INF=int(1e18) dist=[[INF for _ in range(W)] for _ in range(H)] for (_,i,j) in v: if dist[i][j]=A[ni][nj]:continue if dist[pi][pj]-1