program takmicenjeb;
type
matrica=array [1..2000,1..2000] of longint;
var
m:matrica;
br,n,k,i,j,s:longint;
begin
br:=0;
readln(n,k);
for i:=1 to n do begin
for j:=1 to n do begin
repeat
readln(m[i,j]) until m[i,j]<=k end; end;
for i:=1 to n do begin
for j:=1 to n do begin
for s:=2 to n do begin
if (m[i,j]=m[i+1,j]) or (m[i,j]=m[i,j+1]) then br:=br+1;
if (m[i,j]=m[i+s,j]) or (m[i,j]=m[i,j+s]) then br:=br-1;
end; end; end;
writeln(br);
end.