import sys from itertools import permutations from heapq import heappop,heappush from collections import deque import random import bisect input = lambda :sys.stdin.readline().rstrip() mi = lambda :map(int,input().split()) li = lambda :list(mi()) H,W = mi() A = [li() for i in range(H)] INF = 10**9 dp = [[-INF]*W for i in range(H)] idx = [(i,j) for i in range(H) for j in range(W)] idx.sort(key=lambda x:A[x[0]][x[1]]) for (i,j) in idx[::-1]: dp[i][j] = 1 for dx,dy in [(-1,0),(0,1),(1,0),(0,-1)]: ni,nj = i+dx,j+dy if 0 <= ni < H and 0 <= nj < W and dp[ni][nj]!=-INF and A[ni][nj] > A[i][j]: dp[i][j] = max(dp[i][j],dp[ni][nj]+1) res = max(max(dp[i]) for i in range(H)) print(res)