#include<bits/stdc++.h>
#define pb push_back
#define pf push_front
#define fr first
#define sc second
#define FOR(i,x,y) for(int i=x; i<=y; i++)
using namespace std;
void cepi(bool k)
{
//	#ifndef ONLINE_JUDGE
	if(k)freopen("input.txt", "r", stdin);
//	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cerr.tie(nullptr);
}
const int maxn=2001;
const int maxbr=(int)1e6;
bool jeste;
pair<pair<int,int>,pair<int,int>>a[maxn];
int n,k,m[maxn][maxn],sol,q[maxbr],br;
int main()
{
  cepi(0);
  cin>>n>>k;
	FOR(i,1,n) FOR(j,1,n)
	{
		cin>>m[i][j];
		q[m[i][j]]++;
		if(a[m[i][j]].fr.fr==0)
	  {
			a[m[i][j]]={{i,j},{0,0}};
			continue;
		}
		a[m[i][j]]={{a[m[i][j]].fr.fr,a[m[i][j]].fr.sc},{i,j}};
	}
	//for(int i=1;i<=k;i++) cout<<q[i];
	for(int i=1;i<=k;i++)
	{
	//	cout<<i<<"-"<<a[i].fr.fr<<" "<<a[i].fr.sc<<"   "<<a[i].sc.fr<<" "<<a[i].sc.sc<<endl;
		jeste=true;
		br=0;
		for(int j=a[i].fr.fr;j<=a[i].sc.fr;j++)
		for(int l=a[i].fr.sc;l<=a[i].sc.sc;l++)
		{
			br++;
			if(m[j][l]!=i){jeste=false;}
		}
		//if(jeste) cout<<i<<" "<<br<<endl;
		if(jeste && (q[i]==br || q[i]==1)) {sol++;/*cout<<i;*/}
	}
	cout<<sol;


	return 0;
}