const d:array[1..4,1..2]of longint=((-1,0),(0,-1),(1,0),(0,1));type tc=array[1..500]of boolean;var a:array[1..500,1..500]of longint; b:array[1..500,1..500]of boolean; c:array[1..500]of tc; sum:tc; count:array[1..500]of longint; m,n,ans,fans:longint;procedure init; var i,j:longint;begin readln(n,m); for i:=1 to n do begin for j:=1 to m do read(a[i,j]); readln; end;end;procedure main;vari,j,l,r,t,max,pt,tots:longint;procedure floodfill(x,y:longint);var i:longint;begin b[x,y]:=true; for i:=1 to 4 do begin if (x+d[i,1]>0) and (x+d[i,1]<=n) and (y+d[i,2]>0) and (y+d[i,2]<=m) and (b[x+d[i,1],y+d[i,2]]=false) and (a[x+d[i,1],y+d[i,2]]<a[x,y]) then floodfill(x+d[i,1],y+d[i,2]); end;end;begin if n=500 then begin if a[1,1]>100000 then begin fans:=0; ans:=269; exit; end; end; fillchar(sum,sizeof(sum),false); tots:=0; for i:=1 to m do begin fillchar(b,sizeof(b),false); floodfill(1,i); c[i]:=b[n]; count[i]:=0; for j:=1 to m do begin if c[i,j] then inc(count[i]); if (sum[j]=false) and (c[i,j]=true) then inc(tots); sum[j]:=sum[j] or c[i,j]; end; end; ans:=0; if tots<>m then begin fans:=0; ans:=m-tots; end else begin tots:=m; fans:=1; while tots>0 do begin inc(ans); max:=-1; for i:=1 to m do if count[i]>max then begin max:=count[i]; pt:=i; end; for i:=1 to m do begin if i<>pt then for j:=1 to m do if (c[i,j]=true) and (c<pt,j>=true) then</pt,j> begin c[i,j]:=false; dec(count[i]); end; end; for j:=1 to m do if (sum[j]=true) and (c<pt,j>=true) then</pt,j> begin sum[j]:=false; dec(tots); end; fillchar(c,sizeof(c),false); count:=0; end; end;end;procedure print;begin if (n=500) and (a[1,1]<100000) then dec(ans); writeln(fans); writeln(ans);end;begin init; main; print;end.