#include <bits/stdc++.h>
using namespace std;
struct node{
int high;
int x;
int y;
};
bool cmp(node a,node b){
return a.high>b.high;
}
int maxn,r,c,a[110][110],dp[110][110],use1[4]={0,0,-1,1},use2[4]={1,-1,0,0};
node q[110*110];
int main(){
scanf("%d %d",&r,&c);
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
dp[i][j]=1;
scanf("%d",&a[i][j]);
q[j+(i-1)*r].high=a[i][j];
q[j+(i-1)*r].x=i;
q[j+(i-1)*r].y=j;
}
}
sort(q+1,q+1+(r*c),cmp);
for(int i=1;i<=r*c;i++){
int zx=q[i].x,zy=q[i].y,h=q[i].high;
for(int j=0;j<4;j++){
int hx=zx+use1[j],hy=zy+use2[j];
if(hx>=1 && hx<=r && hy>=1 && hy<=c && a[hx][hy]>h){
dp[zx][zy]=max(dp[zx][zy],dp[hx][hy]+1);
}
}
maxn=max(maxn,dp[zx][zy]);
}
cout << maxn;
return 0;
}