Fading @ 2017-11-01 13:06:09
var i,j,k,l,o,p,e,d,s,r,c:longint;
f:array[0..10000] of longint;
a:array[0..101,0..101] of longint;
cl:array[1..10000,1..3] of longint;
procedure sort(l,r: longint);
var
i,j,x,y: longint;
begin
i:=l;
j:=r;
x:=cl[(l+r) div 2][1];
repeat
while cl[i][1]>x do
inc(i);
while x>cl[j][1] do
dec(j);
if not(i>j) then
begin
y:=cl[i][1];
cl[i][1]:=cl[j][1];
cl[j][1]:=y;
y:=cl[i][2];
cl[i][2]:=cl[j][2];
cl[j][2]:=y;
y:=cl[i][3];
cl[i][3]:=cl[j][3];
cl[j][3]:=y;
inc(i);
j:=j-1;
end;
until i>j;
if l<j then
sort(l,j);
if i<r then
sort(i,r);
end;
function max(a,b:longint):longint;
begin
if a>b then exit(a); exit(b);
end;
function juli(i,j:longint):longint;
begin
exit(abs(cl[i][2]-cl[j][2])+abs(cl[i][3]-cl[j][3]));
end;
begin
readln(r,c);
for i:=1 to r do
for j:=1 to c do begin
inc(p);
read(cl[p][1]);
cl[p][2]:=i;
cl[p][3]:=j;
f[p]:=1;
end;
sort(1,r*c);
for i:=1 to r*c do
for j:=1 to i-1 do
begin
if (cl[j][1]>cl[i][1]) and (juli(i,j)=1) then
f[i]:=max(f[i],f[j]+1);
end;
for i:=1 to r*c do e:=max(e,f[i]);
writeln(e);
end.