#include <iostream>
#include <cstring>
#include <cstdlib>
#define SIZE 101
using namespace std;
int N,M;
int maze[SIZE][SIZE];
int opt[SIZE][SIZE];
bool visit[SIZE][SIZE];
int result;
int direction[4][2]={
0,1,
1,0,
0,-1,
-1,0
};
void init()
{
memset(opt,0,sizeof(opt));
memset(visit,0,sizeof(visit));
}
int max(int x,int y)
{
return x>y?x:y;
}
bool isvalid(int x, int y){
if(x<0||y<0||x>=N||y>=M)
return false;
return true;
}
void Input ()
{
result = 0;
init();
scanf("%d %d",&N,&M);
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
scanf("%d",&maze[i][j]);
}
}
} /* ----- end of function Input ----- */
void dfs(int x,int y)
{
//if(opt[x][y]>0) return;
if(visit[x][y])
return;
visit[x][y]=true; //forbid searched again:dead loop!
for(int i=0;i<4;i++){
int tx,ty;
tx = x + direction[i][0];
ty = y + direction[i][1];
if(!isvalid(tx,ty)) continue;
dfs(tx,ty);
if(maze[tx][ty]<maze[x][y]){
opt[x][y]=max(opt[tx][ty]+1,opt[x][y]);
}
}
visit[x][y] = false;//allow backtrace!other point can reuse it
}//simply dfs
void Solve ()
{
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
//init();
memset(opt,0,sizeof(opt));
memset(visit,0,sizeof(visit));
dfs(i,j);
result = max(result,opt[i][j]);
}
}
} /* ----- end of function Solve ----- */
void Output ()
{
printf("%d\n",result+1);
} /* ----- end of function Output ----- */
int main ()
{
Input();
Solve();
Output();
return 0;
} /* ---------- end of function main ---------- */
#include <cstring>
#include <cstdlib>
#define SIZE 101
using namespace std;
int N,M;
int maze[SIZE][SIZE];
int opt[SIZE][SIZE];
bool visit[SIZE][SIZE];
int result;
int direction[4][2]={
0,1,
1,0,
0,-1,
-1,0
};
void init()
{
memset(opt,0,sizeof(opt));
memset(visit,0,sizeof(visit));
}
int max(int x,int y)
{
return x>y?x:y;
}
bool isvalid(int x, int y){
if(x<0||y<0||x>=N||y>=M)
return false;
return true;
}
void Input ()
{
result = 0;
init();
scanf("%d %d",&N,&M);
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
scanf("%d",&maze[i][j]);
}
}
} /* ----- end of function Input ----- */
void dfs(int x,int y)
{
//if(opt[x][y]>0) return;
if(visit[x][y])
return;
visit[x][y]=true; //forbid searched again:dead loop!
for(int i=0;i<4;i++){
int tx,ty;
tx = x + direction[i][0];
ty = y + direction[i][1];
if(!isvalid(tx,ty)) continue;
dfs(tx,ty);
if(maze[tx][ty]<maze[x][y]){
opt[x][y]=max(opt[tx][ty]+1,opt[x][y]);
}
}
visit[x][y] = false;//allow backtrace!other point can reuse it
}//simply dfs
void Solve ()
{
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
//init();
memset(opt,0,sizeof(opt));
memset(visit,0,sizeof(visit));
dfs(i,j);
result = max(result,opt[i][j]);
}
}
} /* ----- end of function Solve ----- */
void Output ()
{
printf("%d\n",result+1);
} /* ----- end of function Output ----- */
int main ()
{
Input();
Solve();
Output();
return 0;
} /* ---------- end of function main ---------- */