#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

int n, k;
vector <vector <int> > v;
vector <vector <int> > t;

unordered_set <int> res;
unordered_set <int> invalid;


bool check(int x, int y) {
	bool ok = true;
	int b = v[x][y];
	int x1 = x, y1 = y;
	while(x1 < n && v[x1][y] == b ) {
		x1++;
	}
	while(y1 < n && v[x][y1] == b) {
		y1++;
	}

	for (int i = x; i < x1; i++) {
		for (int j = y; j < y1; j++) {
			if (v[i][j] != b) {
				ok = false;
			} else {
				t[i][j] = 1;
			}
		}
	}
	return ok;
}

int main() {	
	// #ifndef ONLINE_JUDGE
	// freopen("../../in.txt", "r", stdin);
	// #endif

	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n >> k;

	for (int i = 0; i < n; i++) {
		v.push_back({});
		t.push_back({});
		for (int j = 0; j < n; j++) {
			int x;
			cin >> x;
			v[i].push_back(x);
			t[i].push_back(0);
		}
	}


	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {

			if (t[i][j] == 0) {
				if (res.count(v[i][j]) == 1) {
					res.erase(v[i][j]);
					invalid.insert(v[i][j]);
				} else if (invalid.count(v[i][j]) == 0) {
					if (check(i, j)) {
						res.insert(v[i][j]);
					} else {
						res.erase(v[i][j]);
						invalid.insert(v[i][j]);
					}
				}
			}

		}
	}

	cout << res.size() << endl;
	
	return 0;
}