#include <iostream>
#include <vector>
#include <map>
using namespace std;

struct koord {
	int x1, x2, zadnjaKolona;	
};

int main() {
	int n, k;
	
	cin >> n >> k;
	
	vector<int> row(n);
	vector<vector<int> > M(n, row);
	
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			cin >> M[i][j];	
	
	
	map<int, koord> koordinate;
	map<int, bool> dobar;
	
	for (int i = 0; i < n; i++) {
		// za svaku vrstu
		int x1 = 0, x2 = 0, v = M[i][0];
		
		for (int j = 1; j < n; j++) {
			if (M[i][j] == v) x2++;
			else {
				
				map<int, koord>::iterator it = koordinate.find(v);
				
				if (it != koordinate.end()) {
					if (x1 == it->second.x1 && x2 == it->second.x2 && i == it->second.zadnjaKolona + 1) {
						// sve kul
						it->second.zadnjaKolona = i;
					} else {
						dobar[v] = false;
					}
					
				} else {
					koord k;
					k.x1 = x1;
					k.x2 = x2;
					k.zadnjaKolona = i;
					
					koordinate[v] = k;
					dobar[v] = true;
				}
				
				x1 = j;
				x2 = j;
				v = M[i][j];
			}
			
		}
		
		map<int, koord>::iterator it = koordinate.find(v);
				
				if (it != koordinate.end()) {
					if (x1 == it->second.x1 && x2 == it->second.x2 && i == it->second.zadnjaKolona + 1) {
						// sve kul
						it->second.zadnjaKolona = i;
					} else {
						dobar[v] = false;
					}
					
				} else {
					koord k;
					k.x1 = x1;
					k.x2 = x2;
					k.zadnjaKolona = i;
					
					koordinate[v] = k;
					dobar[v] = true;
				}
	
	}
	
	int dobri = 0;
	
	for (map<int, bool>::iterator it = dobar.begin(); it != dobar.end(); ++it)
		if (it->second)
			dobri++;

	cout << dobri << endl;
	
	return 0;
}