from collections import deque H, W = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(H)] indeg = [0] * (H * W) dh = [0, 1, 0, -1] dw = [1, 0, -1, 0] for h in range(H): for w in range(W): for k in range(4): nh = h + dh[k] nw = w + dw[k] if nh < 0 or nh >= H or nw < 0 or nw >= W : continue if A[h][w] < A[nh][nw]: indeg[nh * W + nw] += 1 dp = [0] * (H * W) nxt = deque() for i in range(H * W): if indeg[i] == 0 : nxt.append(i) while nxt: pos = nxt.popleft() h = pos // W w = pos % W for k in range(4): nh = h + dh[k] nw = w + dw[k] if nh < 0 or nh >= H or nw < 0 or nw >= W : continue if A[h][w] < A[nh][nw]: t = nh * W + nw indeg[t] -= 1 if indeg[t] == 0 : nxt.append((t)) dp[t] = max(dp[t], dp[pos] + 1) print(max(dp) + 1)