#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

bool pogodna(bool matrica[100][100], int n)
{
	int br=0;
	for (int i = 0; i < n-1; i++)
	{
		for (int j = 0; j < n-1; j++)
		{
			if (matrica[i][j] == 1)
			{
				br++;
				if (matrica[i + 1][j + 1] == 1 && !(matrica[i + 1][j] == 1 && matrica[i][j + 1]))
				{
					return false;
				}
			}
		}
	}
	if (br == 0) return false;
	if (br == 1) return true;
	return true;
}

int main()
{
	int n, k, br = 0;
	cin >> n; 
	cin >> k;

	int** matrica = new int*[n];
	// k matrica od 0 ili 1

	// k, i, j
	bool matrice[100][100][100];

	for (int x = 0; x < k; x++)
	{
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				matrice[x][i][j] = 0;
			}
		}
	}

	for (int i = 0; i < n; i++)
		matrica[i] = new int[n];

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

	for (int x = 0; x < k; x++)
	{
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (matrica[i][j] == x+1)
					matrice[x][i][j] = 1;
			}
		}
	}

	for (int x = 0; x < k; x++)
	{
		if (pogodna(matrice[x], n)) br++;
	}
	
	cout << br;

    return 0;
}