import collections,sys,math,functools,operator,itertools,bisect,heapq,decimal,string,time,random #sys.setrecursionlimit(10**9) #sys.set_int_max_str_digits(0) input = sys.stdin.readline #alist = list(map(int,input().split())) #alist = [] #s = input() h,w = map(int,input().split()) #for i in range(n): # alist.append(list(map(int,input().split()))) grid = [list(map(int,input().split())) for i in range(h)] dp = [[1 for i in range(w)] for j in range(h)] hq = [] for i in range(h): for j in range(w): heapq.heappush(hq,(grid[i][j],i,j)) dxy = [[-1,0],[1,0],[0,-1],[0,1]] while hq: x,i,j = heapq.heappop(hq) for dx,dy in dxy: if 0 <= i+dx < h and 0 <= j+dy < w and grid[i+dx][j+dy] > x: dp[i+dx][j+dy] = max(dp[i+dx][j+dy],dp[i][j]+1) ans = 0 for i in dp: ans = max(ans,max(i)) print(ans)