#include <iostream>

using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	unsigned short n;
	cin >> n;
	unsigned short k;
	cin >> k;
	unsigned int m[200][200];

	for (unsigned short i = 0; i < n; ++i)
		for (unsigned short j = 0; j < n; ++j)
			cin >> m[i][j];

	bool neg[1000][2] = { false };
	bool postoji[1000] = { false };

	for (unsigned short i = 0; i < n; ++i)
		for (unsigned short j = 0; j < n; ++j)
		{
			
			if (neg[m[i][j]][1]) neg[m[i][j]][0] = true;
			if (m[i][j] != m[i][j - 1] && m[i][j] != m[i][j + 1] && m[i][j] != m[i - 1][j] && m[i][j] != m[i + 1][j])
			{
				if (postoji[m[i][j]]) neg[m[i][j]][0] = true;
			}
			if (m[i][j] == m[i - 1][j] && m[i][j] == m[i - 1][j - 1] && m[i][j] != m[i][j - 1])
			{
				neg[m[i][j]][0] = true;
				postoji[m[i][j]] = true;
				continue;
			}
			if (m[i][j] == m[i - 1][j] && m[i][j] == m[i - 1][j + 1] && m[i][j] != m[i][j + 1])
			{
				neg[m[i][j]][0] = true;
				postoji[m[i][j]] = true;
				continue;
			}

			if (m[i][j] == m[i + 1][j] && m[i][j] == m[i + 1][j - 1] && m[i][j] != m[i][j - 1])
			{
				neg[m[i][j]][0] = true;
				postoji[m[i][j]] = true;
				continue;
			}
			if (m[i][j] == m[i + 1][j] && m[i][j] == m[i + 1][j + 1] && m[i][j] != m[i][j + 1])
			{
				neg[m[i][j]][0] = true;
				postoji[m[i][j]] = true;
				continue;
			}

			if (m[i][j] == m[i][j - 1] && m[i][j] == m[i - 1][j - 1] && m[i][j] != m[i - 1][j])
			{
				neg[m[i][j]][0] = true;
				postoji[m[i][j]] = true;
				continue;
			}
			if (m[i][j] == m[i][j - 1] && m[i][j] == m[i + 1][j - 1] && m[i][j] != m[i + 1][j])
			{
				neg[m[i][j]][0] = true;
				postoji[m[i][j]] = true;
				continue;
			}

			if (m[i][j] == m[i][j + 1] && m[i][j] == m[i - 1][j + 1] && m[i][j] != m[i - 1][j])
			{
				neg[m[i][j]][0] = true;
				postoji[m[i][j]] = true;
				continue;
			}
			if (m[i][j] == m[i][j + 1] && m[i][j] == m[i + 1][j + 1] && m[i][j] != m[i + 1][j])
			{
				neg[m[i][j]][0] = true;
				postoji[m[i][j]] = true;
				continue;
			}
			postoji[m[i][j]] = true;
		}

	for (unsigned short i = 1; i < k + 1; ++i)
		if (!neg[i][0] && postoji[i]) std::cout << i << endl;

	std::cout << endl;
	return 0;
}