#include <stdio.h>
int mat[2000][2000];

struct tacka
{
	int x; int y;
};
struct sorta
{
	sorta() : init(false) {};
	bool init;
	bool valid;
	tacka gornji_l;
	int xstr; int ystr;
	int count;
};
sorta sorte[1000000];
int main()
{
	int n, k;
	scanf("%d %d", &n, &k);
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			int val;
			scanf("%d", &val);
			mat[i][j] = val;
			if (sorte[val].init)
			{
				if (j == sorte[val].gornji_l.x)
				{
					sorte[val].ystr++;
				}
				else if (i == sorte[val].gornji_l.y)
				{
					sorte[val].xstr++;
				}
				if (j < sorte[val].gornji_l.x)
				{
					sorte[val].valid = false;
				}
				if (j >= sorte[val].gornji_l.x+sorte[val].xstr)
				{
					sorte[val].valid = false;
				}
				if (i >= sorte[val].gornji_l.y+sorte[val].ystr)
				{
					sorte[val].valid = false;
				}
				if (i < sorte[val].gornji_l.y + sorte[val].ystr && j >= sorte[val].gornji_l.x && j < sorte[val].gornji_l.x + sorte[val].xstr)
				{
					sorte[val].count++;
				}
			}
			else
			{
				sorte[val].init = true;
				sorte[val].gornji_l.x = j; sorte[val].gornji_l.y = i;
				sorte[val].valid = true;
				sorte[val].xstr = 1; sorte[val].ystr = 1;
				sorte[val].count = 1;
			}
		}
	}
	int count = 0;
	for (int i = 0; i <= k; i++)
	{
		if (sorte[i].init && sorte[i].valid && (sorte[i].xstr*sorte[i].ystr == sorte[i].count))
		{
			count++;
		}
	}
	printf("%d", count);
	return 0;
}