H,W = map(int,input().split()) A = [list(map(int,input().split())) for _ in range(H)] pairs = [] for i in range(H): for j in range(W): pairs.append((A[i][j],i,j)) pairs.sort(reverse=True) dp = [[-1]*W for _ in range(H)] dij = [(-1,0),(1,0),(0,-1),(0,1)] for a,i,j in pairs: maximum = 0 for di,dj in dij: if 0 <= i+di <= H-1 and 0 <= j+dj <= W-1 and a < A[i+di][j+dj]: maximum = max(maximum,dp[i+di][j+dj]) dp[i][j] = maximum+1 print(max([max(li) for li in dp]))