#include <bits/stdc++.h>
using namespace std;
int main()
{
	int br,p,n,k,d,g,i,j,f;
	br=0;
	scanf("%d",&n);
	scanf("%d",&k);
	int **A=new int *[n+1];
	for (int i = 0 ; i < n+1 ; i++)
    A[i] = new int[n+1];
    vector<int> pom;
	for (i=0;i<=k;i++)
	{
		pom.push_back(0);
	}
	vector<int> X;
	for (i=0;i<=k;i++)
	{
		X.push_back(0);
	}
	vector<int> Y;
	for (i=0;i<=k;i++)
	{
		Y.push_back(0);
	}
	for (i=0;i<n;i++)
	{
		for (j=0;j<n;j++)
		{
			scanf("%d",&A[i][j]);
			pom[A[i][j]]++;
			if (pom[A[i][j]]==1)
			{
				X[A[i][j]]=j;
				Y[A[i][j]]=i;
			}
		}
	}
	
	d=0;
	g=1;
	for (i=1;i<=k;i++)
	{
	 if (pom[i]!=0)
	 	{	
			while((A[Y[i]][X[i]]==A[Y[i]][X[i]+d]) and (X[i]+d<n))
			{
				d++;
	 		} 
	 		gore:
	 		for (j=X[i];j<=X[i]+d-1;j++)
	 		{
	 			if (A[Y[i]+g][X[i]]!=A[Y[i]][X[i]])
	 			{
	 				POVRSINA:
	 				p=g*d;
	 				goto dole;
				 }
				 
				 
				 	if (A[Y[i]+g][j]!=A[Y[i]][X[i]])
				 	{
				 		goto dalje;
					 }
				 
			 }
			 if (Y[i]+g<n)
			 {
			 	g++;
			 	goto gore;
			 }
		}
		dole:
		if (p==pom[i])
		{
			br++;
		}
		d=0;
		g=1;
		dalje:
			f=0;
	}
	printf("%d",br);
	
}