def Map(): return list(map(int,input().split())) H,W = Map() A = [Map() for i in range(H)] G = [[] for i in range(H*W)] dir = [(-1,0),(1,0),(0,1),(0,-1)] D = [0 for i in range(H*W)] for h in range(H): for w in range(W): u = h*W + w for dh,dw in dir: hh,ww = h + dh,w + dw if 0 <= hh < H and 0 <= ww < W: v = hh*W + ww if A[h][w] < A[hh][ww]: G[v].append(u) D[u] += 1 dp = [0 for i in range(H*W)] S = [] for i in range(H*W): if D[i] == 0: S.append(i) dp[i] = 1 while len(S): v = S.pop() for u in G[v]: D[u] -= 1 dp[u] = max(dp[u],dp[v] + 1) if D[u] == 0: S.append(u) print(max(dp))