#include <bits/stdc++.h>
using namespace std;
struct Rect{
int xmax,ymax,xmin,ymin;

};
Rect Def(){

Rect x;
x.xmax=-1;
x.ymax=-1;
x.xmin=10000000;
x.ymin=10000000;
return x;
}
 vector<vector <bool> > Bio;
vector <int> Unique;
vector<vector<int> > a;
vector <Rect> Rects;
vector <int> Count;
int n,k;
void DFS(int x,int y,int color){
if(x<0||y<0||x>=n||y>=n)return;
if(a[x][y]!=color)return;
if(Bio[x][y])return;
Rects[color].xmax=max(x,Rects[color].xmax);
Rects[color].ymax=max(y,Rects[color].ymax);
Rects[color].xmin=min(x,Rects[color].xmin);
Rects[color].ymin=min(y,Rects[color].ymin);
Bio[x][y]=1;
DFS(x+1,y,color);
DFS(x,y+1,color);
DFS(x-1,y,color);
DFS(x,y-1,color);
}
int main(){

cin >>n>>k;
vector <int> vi(n);
a.resize(n,vi);

vector <bool> vb(n,0);
Bio.resize(n,vb);
Unique.resize(k,0);
Rects.resize(k,Def());

Count.resize(k);
for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
        cin >>a[i][j];
        a[i][j]--;
        Count[a[i][j]]++;

    }
}

for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
        if(Unique[a[i][j]]==0){
            if(!Bio[i][j]){
                DFS(i,j,a[i][j]);
                Unique[a[i][j]]=1;
            }
        }
        else{if(!Bio[i][j]){
    Unique[a[i][j]]=-1;
   // cout<<"OU"<<endl;
   }


        }
    }
}
int rez=0;
for(int i=0;i<k;i++){

if(Unique[i]==-1)continue;
if(Unique[i]==0){//cout <<"000000 "<<i<<endl;
    continue;}

int P=(Rects[i].xmax-Rects[i].xmin+1)*(Rects[i].ymax-Rects[i].ymin+1);
//cout <<P<<endl;

if(P==Count[i])rez++;
}
cout <<rez<<endl;
}