program project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this };
type
  matrica=array[1..100,1..100] of integer;

  procedure citaj(n:integer;var a:matrica);
  var
      i,j:integer;
      begin
        for i:=1 to n do
        begin
          for j:=1 to n do
          read(a[i,j]);
          readln;
          end;
        end;
var
    ok:boolean;
    b:matrica;
    brv,id,jd,idl,jdl,idd,jdd,ii,ji,bt,bi,bj:integer;
    n,i,j,i1,j1,zi,zj:integer;
    v,k:longint;
begin
  readln(n,k);
  citaj(n,b);

  while (v<=k) do

  begin
  ok:=false;
  brv:=0;

  for i:=1 to n do
  begin
  for j:=1 to n do
  begin
  if (b[i,j]=v) then             //Nalazi kada se prvi put javlja u matrici
  begin
  i1:=i; j1:=j;
  bi:=1;
  bj:=1;
                       //ispitivanje do kog elemenata na desno
    while bj=1 do
    if (b[i,j1]=v) then
    begin
    zj:=j1;
    j1:=j1+1
    end else begin bj:=0; break;end;

    while bi=1 do
    if (b[i1,j]=v) then
    begin
    zi:=i1 ;
    i1:=i1+1
    end else  begin bi:=0;  break;end;    //ispitivanje do kog elemenata na dole


    if ((zi=i) and (zj=j)) then
    begin
    ok:=true;
    if not (j=1) then
    begin
    for idl:=i+1 to zi do                 //ispitivanje dole levo
    for jdl:=1 to j-1  do
    if b[idl,jdl]=v then ok:=false;
    end;

    if not (j=n) then
    begin
    for idd:=i to zi do                   //ispitivanje desno
    for jdd:=zj+1 to n do
    if b[idd,jdd]=v then ok:=false;
    end;

    ii:=zi+1;
    if not (ii=n+1) then
    begin
    for ii:=(zi+1) to n do                   //ispitivanje ispod
    for ji:=1 to n do
    if b[ii,ji]=v then ok:=false;
    end;

    end else

    begin

    for id:=i to zi do              // da li su svi elementi na toj povrsini
    for jd:=j to zj do                  //jednaki v
    if (b[id,jd]=v) then ok:=true;

    if ((zi-i)=(zj-j)) then ok:=false;  //ispitivanje da li je kvadrat

    if not (j=1) then
    begin
    for idl:=i+1 to zi do                 //ispitivanje dole levo
    for jdl:=1 to j-1  do
    if b[idl,jdl]=v then ok:=false;
    end;

    if not (j=n) then
    begin
    for idd:=i to zi do                   //ispitivanje desno
    for jdd:=zj+1 to n do
    if b[idd,jdd]=v then ok:=false;
    end;

    ii:=zi+1;
    if not (ii=n+1) then
    begin
    for ii:=(zi+1) to n do                   //ispitivanje ispod
    for ji:=1 to n do
    if b[ii,ji]=v then ok:=false;
    end;



    end;

   brv:=brv+1; end;
   end; if(brv=1) then break;
  end;

  if ok=true then bt:=bt+1;
  
  v:=v+1;
  end;


 
  writeln(bt);


  while true do

end.