#include <iostream>
#include <vector>

bool proveri(std::vector<std::vector<int>> &mat, int py, int px, int ky, int kx)
{
	for (int i = py + 1; i < ky; i++)
		for (int j = px + 1; j < kx; j++)
			if (mat[i][j] != mat[py][px]) return false;
	return true;
}

int main()
{
	int N, K;
	std::cin >> N >> K;
	std::vector<std::vector<int>> M(N, std::vector<int>(N));
	std::vector<int> sorte(K + 1, 0);
	std::vector<bool> bsorte(K + 1, false);
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
		{
			std::cin >> M[i][j];
			sorte[M[i][j]]++;
			bsorte[M[i][j]] = true;
		}
	int brp = 0, x, y;
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			if (bsorte[M[i][j]])
			{
				x = j;
				y = i;
				while (y < N && M[y][j] == M[i][j]) y++;
				while (x < N && M[i][x] == M[i][j]) x++;
				if (proveri(M, i, j, y, x) && (y - i)*(x - j) == sorte[M[i][j]]) brp++;
				bsorte[M[i][j]] = false;
			}
	std::cout << brp;
	return 0;
}