//Template
#include <bits/stdc++.h>
#define INF INT_MAX
#define MOD 100000007
#define MAX_N 2000
#define MAX_K 10000055
using namespace std;

int N, K, A[MAX_N][MAX_N];
int up[MAX_K], down[MAX_K], leftt[MAX_N], rightt[MAX_N], total[MAX_K];
int main(){
	cin>>N>>K;
	for(int i=0; i<N; ++i){
		for(int j=0; j<N; ++j){
			scanf("%d", &A[i][j]);
			A[i][j]--;
		}
	}
	for(int i=0; i<K; ++i){
		up[i]=N;
		down[i]=-1;
		leftt[i]=N;
		rightt[i]=-1;
		total[i]=0;
	}
	for(int i=0; i<N; ++i){
		for(int j=0; j<N; ++j){
			total[A[i][j]]++;
			if(up[A[i][j]]>i)up[A[i][j]]=i;
			if(leftt[A[i][j]]>j)leftt[A[i][j]]=j;
			if(down[A[i][j]]<i)down[A[i][j]]=i;
			if(rightt[A[i][j]]<j)rightt[A[i][j]]=j;
		}
	}
	/*for(int i=0; i<K; ++i){
		cout<<i<<":"<<endl;
		printf("%d %d %d %d %d\n", up[i], down[i], leftt[i], rightt[i], total[i]);
	}*/
	int b=0;
	for(int i=0; i<K; ++i){
		if(total[i]>0 && total[i]==(down[i]-up[i]+1)*(rightt[i]-leftt[i]+1) ){
			b++;
		}
	}
	cout<<b;
	return 0;
}