program Project1;

var n,k,i,j,meg:longint;
    h:array[1..100,1..100]of longint;

procedure ter(v:integer);
var xmin,xmax,ymin,ymax:integer;
    test:boolean;
begin

ymin:=n+1;
ymax:=0;
xmin:=n+1;
xmax:=0;

for i:=1 to n do
 for j:=1 to n do
  if (h[i,j]=v) then
   begin
   if (i<ymin) then ymin:=i;
   if (i>ymax) then ymax:=i;
   if (j<xmin) then xmin:=j;
   if (j>xmax) then xmax:=j;
   end;

test:=true;

if (xmin=n+1)and(xmax=0)and(ymin=n+1)and(ymax=0) then test:=false;

for i:=ymin to ymax do
 for j:=xmax to xmax do
  if (h[i,j]<>v) then test:=false;

if (test=true) then meg:=meg+1;

v:=v+1;

if (v<=k) then ter(v);

end;

begin

read(n,k);

for i:=1 to n do
 for j:=1 to n do
  read(h[i,j]);

ter(1);

writeln(meg);

end.