H,W=map(int,input().split()) A=[list(map(int,input().split())) for h in range(H)] dp=[[1]*W for h in range(H)] AHW=[(A[h][w],h,w) for h in range(H) for w in range(W)] AHW.sort() for a,h,w in AHW: for hh,ww in ((h-1,w),(h+1,w),(h,w-1),(h,w+1)): if 0<=hh