#include <bits/stdc++.h> 
#define f first
#define s second
#define pb push_back
#define mp make_pair

using namespace std;

bool okej[1000001];
int mat[2000][2000];
int pojave[1000001] = {0};
bool provereno[1000001] = {0};
bool poseceno[2000][2000] = {{false}};

int n, k;

int main()
{
	ios::sync_with_stdio(false);	
	cin.tie(0);	
	
	memset(okej, true, sizeof(okej));
	
	cin >> n >> k;	
	for(int i = 0; i < n; i++)
	{	
		for(int j = 0; j < n; j++) 
		{
			cin >> mat[i][j];
			pojave[mat[i][j]]++;
		}
	}

	
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			if(poseceno[i][j]) continue;
			if(!provereno[mat[i][j]])
			{				
				int desno = j+1, redova = 1, red = i+1;
				while(mat[i][desno] == mat[i][j] && desno < n)
				{
					poseceno[i][desno] = true;
					desno++;
				} 
				while(red < n)
				{
					bool moze = true;
					for(int k = j; k < desno; k++)
					{					
						if(mat[red][k] != mat[i][j]) {
							moze = false;
							break;
						}						
					}
					if(!moze) break;
					for(int k = j; k < desno; k++) poseceno[red][k] = true;
					redova++;
					red++;
				}
				// cout << "Prav " << mat[i][j] << " " << (desno - j) << "x" << redova << endl;
				provereno[mat[i][j]] = true;
			}
			else if(okej[mat[i][j]]) okej[mat[i][j]] = false;
		}
	}
	int rez = 0;
	for(int i = 1; i <= k; i++)
	{
		if(okej[i] && pojave[i] != 0) 
		{
			// cout << i << " ";	
			rez++;
		}
	} 
	//cout << endl;
	cout << rez;
	return 0; 
}