#include<iostream>



int main()
{
	int val[101][101];
	char data[101][101];
	char dobar[100009];
	int N, K;
	int dobri = 0;
	std::cin >> N >> K;

	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			std::cin>>val[j][i];
			data[j][i] = 0;
		}
	}

	for (int j = 0; j < K+5; j++)
	{
		dobar[j] = 0;
	}


	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			if (data[j][i] == 1)
				continue;

			

			int current = val[j][i];

			if (dobar[current] == 1)
			{
				dobar[current] = 2;
				dobri--;
				continue;
			}
			else if (dobar[current] == 2)
				continue;

			dobar[current] = 1;
			dobri++;

			int cX=j,cY=i;



			while (true)
			{
				
				cX++;
				if (cX >= N)
				{
					
					cX--;
					break;
				}
					
				if (!(val[cX][cY] == current))
				{
					cX--;
					break;
				}
			}
			while (true)
			{
				for (int c = j; c <= cX; c++)
				{
					data[c][cY] = 1;

				}


				cY++;
				if (cY >= N)
				{
					cY--;
					break;
				}

				bool goodRow=true;
				for (int c = j; c <= cX; c++)
				{
					if (!(val[c][cY] == current))
					{
						goodRow = false;
						break;
					}
					
				}

				if (!goodRow)
				{
					cY--;
					break;
				}


			}
			j = cX;


		}
	}

	std::cout << dobri;


	
	return 0;
}