#include <iostream>
#define MAXN 2001
#define MAXK 1000001
using namespace std;

int n, k, m[MAXN][MAXN];
int xl[MAXK], yl[MAXK], xr[MAXK], yr[MAXK];
int br[MAXK];

int min(int x, int y)
{
	return x < y ? x : y;
}

int max(int x, int y)
{
	return x > y ? x : y;
}

int main()
{
	int n, k;
	cin >> n >> k;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			cin >> m[i][j];

	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
		{
			int vrsta = m[i][j];
			if (br[vrsta])
			{
				xl[vrsta] = min(xl[vrsta], i);
				xr[vrsta] = max(xr[vrsta], i);
				yl[vrsta] = min(yl[vrsta], j);
				yr[vrsta] = max(yr[vrsta], j);
			}
			else
			{
				xl[vrsta] = i;
				xr[vrsta] = i;
				yl[vrsta] = j;
				yr[vrsta] = j;
			}
			br[vrsta]++;
		}

	int sol = 0;
	for (int i = 1; i <= k; i++)
		if ((xr[i] - xl[i] + 1) * (yr[i] - yl[i] + 1) == br[i])
			sol++;
	cout << sol << endl;

	return 0;
}