#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int **input(int n, int m) {
	int **x = new int*[n];
	for (int i = 0; i < n; i++) {
		x[i] = new int[m];
		for (int j = 0; j < m; j++)
			scanf("%d", x[i] + j);
	}
	return x;
}

void output(int n, int m, int **x) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++)
			printf("%d ", x[i][j]);
		putchar('\n');
	}
}

int **build(int k, int n, int **x) {
	int **t = new int*[5];
	for (int i = 0; i < 5; i++)
		t[i] = new int[k];
	for (int i = 0; i < k; i++) {
		t[0][i] = 0; 
		for (int j = 1; j < 5; j++)
			t[j][i] = -1;
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int p = x[i][j] - 1;
			t[0][p]++;
			if (t[1][p] == -1 || i < t[1][p])
				t[1][p] = i;
			t[2][p] = max(t[2][p], i);
			if (t[3][p] == -1 || j < t[3][p])
				t[3][p] = j;
			t[4][p] = max(t[4][p], j);
		}
	}
	return t;
}

int count(int k, int n, int **x) {
	int **t = build(k, n, x), s = 0;
	//output(5, k, t);
	for (int i = 0; i < k; i++)
		if (t[0][i] == (t[2][i] - t[1][i] + 1)*(t[4][i] - t[3][i] + 1)) {
			s++;
			//cout << i;
		}
	return s;
}

int main() {
	int n, k, **x;
	cin >> n >> k;
	x = input(n, n);

	cout << count(k, n, x);

	for (int i = 0; i < n; i++)
		delete[] x[i];
	delete[] x;
	return 0;
}