#include <iostream>
#include <vector>

using namespace std;

bool ispitaj(int x, int y, int a[2000][2000], int k)
{
	if(a[x-1][y-1]==k)
	{
		if(a[x][y-1]==k && a[x-1][y]==k)
		{
			if((a[x+1][y-1]!=k && a[x+1][y]!=k && a[x-1][y+1]!=k && a[x][y+1]!=k && a[x+1][y+1]!=k) //cosak gore levo
				|| (a[x+1][y-1]==k && a[x+1][y]==k && a[x-1][y+1]==k && a[x][y+1]==k && a[x+1][y+1]==k) //sve popunjeno
				|| (a[x+1][y-1]!=k && a[x+1][y]!=k && a[x-1][y+1]==k && a[x][y+1]==k && a[x+1][y+1]!=k) //leva strana
				|| (a[x+1][y-1]==k && a[x+1][y]==k && a[x-1][y+1]!=k && a[x][y+1]!=k && a[x+1][y+1]!=k)) //gornja strana
				return true;
			else 
				return false;
		}
		else return false;
	}
	else if(a[x][y-1]==k)
	{
		if(a[x+1][y-1]==k && a[x+1][y]==k)
		{
			if((a[x-1][y]!=k && a[x-1][y+1]!=k && a[x][y+1]!=k && a[x+1][y+1]!=k) //cosak gore desno
				|| (a[x-1][y]!=k && a[x-1][y+1]!=k && a[x][y+1]==k && a[x+1][y+1]==k)) //desna strana
				return true;
			else 
				return false;
		}
		else if(a[x+1][y-1]!=k && a[x-1][y]!=k && a[x+1][y]!=k && a[x-1][y+1]!=k && a[x+1][y+1]!=k) return true; //srednja vertikalna linija i gornja mala linija
		else return false;
	}
	//else if(a[x+1][y-1]==k)
	//{
	//	if(a[x+1][y]==k && a[x+1][y+1]==k && a[x][y+1]!=k && a[x-1][y]!=k && a[x-1][y+1]!=k) return true; //desna vertikalna linija
	//	else return false;
	//}
	else if(a[x-1][y]==k)
	{
		if(a[x-1][y+1]==k && a[x][y+1] && a[x+1][y+1]!=k && a[x+1][y]!=k && a[x+1][y-1]!=k) return true; //donji levi cosak
		else if(a[x-1][y+1]==k && a[x][y+1] && a[x+1][y+1]==k && a[x+1][y]==k && a[x+1][y-1]!=k) return true; //donja strana
		else if(a[x-1][y+1]!=k && a[x][y+1]!=k && a[x+1][y+1]!=k && a[x+1][y-1]!=k) return true; //srednja horizontalna linija i leva mala linija
		else return false;
	}
	else if(a[x+1][y]==k)
	{
		if((a[x+1][y+1]==k && a[x][y+1]==k && a[x-1][y+1]!=k)) return true; //donji desni cosak
		else if((a[x+1][y+1]!=k && a[x][y+1]!=k && a[x-1][y+1]!=k)) return true; //desna mala linija
		else return false;
	}
	else if(a[x][y+1]==k)
	{
		if(a[x-1][y+1]!=k && a[x+1][y+1]!=k && a[x+1][y-1]!=k) return true; //donja mala linija
		else return false;
	}
	else if(a[x+1][y-1]!=k && a[x-1][y+1]!=k && a[x+1][y+1]!=k) return true; //sve prazno
	else return false;
}
int main()
{
	int a[2000][2000];
	int n, k, resenje = 0, p, q;
	bool r = false;
	bool z = false;
	vector<bool> vec;
	cin >> n;
	cin >> k;
	for(int i = 0; i <= k; i++) vec.push_back(false);
	for(int i = 1; i <= n; i++)
	{
	    for(int j = 1; j <= n; j++)
	    {
	        cin >> a[i][j];
			if(vec[a[i][j]] == false)
			{
				vec[a[i][j]] = true;
				resenje++;
			}
	    }
	}
	for(int i = 1; i <= k; i++)
	{
		if(vec[i]==false) continue;
		z = false;
		p = 1; q = 1;
		for(int x = 1; x <= n; x++)
		{
			for(int y = 1; y <= n; y++)
			{
				if(a[x][y]==i)
				{
					if(ispitaj(x,y,a,i) == false || ((x>p+1 || x<p-1 || y>q+1) && z==true))
					{
					    resenje--;
			 			r = true;
						z = false;
						break;
					}
					z=true;
					p = x; q = y;
				}
			}
			if(r)
			{
				r = false;
				break;
			}
		}
	}
	cout << resenje;
	return 0;
}