#include <bits/stdc++.h>
using namespace std;

#define long int64_t
#define endl '\n'

#ifdef OFFLINE
	#define logan(...) logg(#__VA_ARGS__, __VA_ARGS__)
	template<class Arg>
	void logg(const char* name, Arg&& arg){ cerr << name << ": " << arg << '\n'; }
	template<class Arg, class ... Args>
	void logg(const char* name, Arg&& arg, Args&& ... args){
		const char* next = strchr(name+1, ',');
		cerr.write(name, next-name) << ": " << arg << ",";
		logg(next+1, args...);
	}
	#define timeme 1.0*clock()/CLOCKS_PER_SEC
#else
	#define logan(...) {}
	#define timeme {}
#endif

int a[2001][2001];
int b[2001][2001];
int smrc[1'000'001];
int cnt[1'000'001];
basic_string<int> g;

void kocegazna(int n){
	for(int i = 0; i < n; ++i){
		for(int j = 0; j < n; ++j){
			if(b[i][j]) continue;
			if(smrc[a[i][j]]) continue;


			int cur = a[i][j];
			int maxi = 0, maxj = 0;
			for(maxi = 0; i+maxi < n && a[i+maxi][j] == cur; ++maxi);
			for(maxj = 0; j+maxj < n && a[i][j+maxj] == cur; ++maxj);

			int ok = 1;
			for(int _i = 0; _i < maxi && ok; ++_i)
			for(int _j = 0; _j < maxj && ok; ++_j){
				if(a[i+_i][j+_j] != cur){ ok = 0; smrc[cur] = 1; break; }
				b[i+_i][j+_j] = 1;
			}
			if(ok) g += cur;
		}
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cerr.tie(nullptr);

	int n, k; cin >> n >> k;
	for(int i = 0; i < n; ++i){
		for(int j = 0; j < n; ++j){
			cin >> a[i][j];
		}
	}
	kocegazna(n);

	#ifdef OFFLINE
		for(auto& i : g) cerr << i << ' '; cerr << endl;
	#endif

	int pows = 0;
	for(auto& i : g) ++cnt[i];
	for(int i = 1; i <= k; ++i){
		if(!cnt[i] || cnt[i] > 1) continue;
		++pows;
	}
	cout << pows << endl;

}