h,w=map(int,input().split()) a=[list(map(int,input().split()))for i in range(h)] t=[] for i in range(h): for j in range(w): t.append((a[i][j], i, j)) t.sort() t=t[::-1] dp=[[0]*w for i in range(h)] for val, i, j in t: dp[i][j] = 1 for v in [(i-1, j),(i+1, j),(i, j-1),(i, j+1)]: if 0 <= v[0] < h and 0 <= v[1] < w and a[v[0]][v[1]] > val: dp[i][j] = max(dp[i][j], dp[v[0]][v[1]] + 1) ans = 0 for i in range(h): for j in range(w): ans = max(ans, dp[i][j]) print(ans)