H,W=map(int,input().split()) A=[list(map(int,input().split())) for _ in range(H)] B=list(range(H*W)) B.sort(key=lambda x:A[x//W][x%W]) D=[[1]*W for _ in range(H)] dd=[(0,1),(1,0),(-1,0),(0,-1)] for p in B: i,j=divmod(p,W) for di,dj in dd: ni=i+di nj=j+dj if 0<=niA[i][j]: D[ni][nj]=max(D[ni][nj],D[i][j]+1) print(max(max(D[i]) for i in range(H)))