#include <bits/stdc++.h>

using namespace std;

#define int ll
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pi> vp;
typedef vector<pl> vpl;

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

	int n, k;
	cin >> n >> k;

	vector<vi> a(n + 1, vi(n + 1, 0));
	vi cnt(k + 1, 0);
	vp l(k + 1, {-1, -1}), r(k + 1, {-1, -1});
	for (int i = 1; i <= n; ++i){
		for (int j = 1; j <= n; ++j){
			int x;
			cin >> x;

			a[i][j] = x;

			++cnt[x];
			r[x] = {i, j};

			if (l[x].first == -1 && l[x].second == -1)
				l[x] = {i, j};
		}
	}

	vector<vi> s(n + 1, vi(n + 1, 0));
	for (int i = 1; i <= n; ++i){
		for (int j = 1; j <= n; ++j){
			s[i][j] = a[i][j] + s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
		}
	}

	int sol = 0;
	for (int i = 1; i <= k; ++i){
		if (!cnt[i])
			continue;

		if (l[i].first > r[i].first || l[i].second > r[i].second)
			continue;

		int c = (r[i].first - l[i].first + 1LL) * 1LL * (r[i].second - l[i].second + 1LL);
		if (c != cnt[i]) continue;

		int x1 = l[i].first, x2 = r[i].first, y1 = l[i].second, y2 = r[i].second;
		int suma = s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1];

		if (suma == cnt[i] * i)
			++sol;		
	}

	cout << sol << '\n';
}