#include <iostream>
#include <string>
#include <math.h>
#include<algorithm>
#include<cstdio>
#include<string.h>
using namespace std;
int f[105][105];
int a[105][105];
struct Node
{
int x;
int y;
int h;
}node[10005];
bool cmp(Node a, Node b)
{
return a.h < b.h;
}
int main()
{
int r, c, ans = 0;
cin >> r >> c;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cin >> a[i][j];
node[i * c + j].h = a[i][j];
node[i * c + j].x = i;
node[i * c + j].y = j;
f[i][j] = 1;
}
}
sort(node, node + r * c, cmp);
for (int i = 0; i < r * c; i++)
{
int r1 = node[i].x;
int c1 = node[i].y;
if (r1 > 0 && a[r1][c1] > a[r1 - 1][c1])
{
f[r1][c1] = max(f[r1][c1], f[r1 - 1][c1] + 1);
}
if (c1 > 0 && a[r1][c1] > a[r1][c1-1])
{
f[r1][c1] = max(f[r1][c1], f[r1][c1-1] + 1);
}
if (r1+1<r && a[r1][c1]>a[r1 + 1][c1])
{
f[r1][c1]= max(f[r1][c1], f[r1+1][c1] + 1);
}
if (c1 + 1 < c && a[r1][c1] > a[r1][c1+1])
{
f[r1][c1] = max(f[r1][c1], f[r1][c1+1] + 1);
}
}
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
ans = max(ans, f[i][j]);
}
}
cout << ans << endl;
return 0;
}