#include <iostream>
using namespace std;

int
main()
{
	int n;
	int k;
	int x[100000];
	int sum = -1;

	cin >> n;
	cin >> k;


	for (int i = 0; i < n * n; i++)
	{
		cin >> x[i];
	}

	//Naci jedinstvene brojeve
	int seen[10000];
	for (int i = 0; i < n; i++)
		seen[i] = 0;

	for (int i = 0; i < n; i++)
	{
		if (seen[i] == 0)
		{
			int count = 0;
			for (int j = i; j < n; j++)
				if (x[j] == x[i])
				{
					count += 1;
					seen[j] = 1;
				}
			if (count == 1)
			{
				sum += 1;
			}
		}
	}


	//Naci pravougaonike

	for (int l = 0; l < n * n; l++)
	{
		int xCounter = 1;
		int yCounter = 1;

		//Provera da li je isti br. desno
		if (x[l] == x[l + 1] && l + 1 <= n*(l + 1))
		{
			xCounter += 1;

			for (int m = l; m < n * n; m++)
			{
				if (x[m + 1] == x[m + 2] && m + 2 <= n*(m + 1))
				{
					xCounter += 1;
				}
				else m=n*n;
			}
		}

		//Provera da li je isti br. dole
		if (x[l] == x[l + n] && l + n <= n * n)
		{
			yCounter += 1;
			for (int m = l; m < n * n; m +=n)
			{
				if (x[m + n] == x[m + n + n] && m + n <= n * n)
				{
					yCounter += 1;
				}
				else m = n*n;
			}
		}
		cout << xCounter << yCounter << endl;
		
		
	    int num = 0;
		for (int z = 0; z < n * n; z++)
		{
			if (x[z] == x[l] && x[z] != 0)
			{
				num++;
			}
		}
		if ((x[l] == x[l - 1] && l >= 1) || (x[l] == x[l - n] && l >= n))
		{

		}
		else
		{
			if (num == xCounter * yCounter && xCounter != yCounter)
			{
				sum++;
			}
		}		
	}



	cout << sum;
}