#include<iostream>
#include<vector>

using namespace std;
int main()
{
	vector<vector<int>> val;
	vector<vector<char>> data;
	vector<char> dobar;
	int N, K;
	int dobri = 0;
	std::cin >> N >> K;
	val.resize(N);
	data.resize(N);



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

	dobar.resize(K + 5);
	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;
}