#include <iostream>
#include<queue>
#include<string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
struct xun
{
int i=0,j=0,num=0;
};
struct cmp1
{
bool operator()(const xun a,const xun b)
{
return a.num>b.num;
}
};
priority_queue<xun,vector<xun>,cmp1>q;
int z[102][102]={};
int total[102][102]={};
int main(int argc, char** argv) {
memset(total,0,sizeof(total));
for(int i=1;i<=101;i++)
for(int j=1;j<=101;j++)
total[i][j]=1;
int a,b;
cin>>a>>b;
for(int i=1;i<=b;i++)
for(int j=1;j<=a;j++)
{
xun asa;
cin>>asa.num;
z[i][j]=asa.num;
asa.i=i;
asa.j=j;
q.push(asa);
}
while(!q.empty())
{
xun p=q.top();
int i=p.i;
int j=p.j;
int num=p.num;
q.pop();
if( num>z[i-1][j] )total[i][j]=max(total[i-1][j]+1,total[i][j]);
if( num>z[i+1][j] )total[i][j]=max(total[i+1][j]+1,total[i][j]);
if( num>z[i][j+1] )total[i][j]=max(total[i][j+1]+1,total[i][j]);
if( num>z[i][j-1] )total[i][j]=max(total[i][j-1]+1,total[i][j]);
}
int xunxas=-1;
for(int i=1;i<=b;i++)
for(int j=1;j<=a;j++)
xunxas=max(xunxas,total[i][j]);
cout<<xunxas;
return 0;
}