#include<stdio.h>
#include<iostream>
#include <algorithm>
#include<math.h>
#define A 2001
#define B 1000001
int sorta[B];
int polje[A][A];
int success[B];
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int i,j,m,provera=1;
	int ii,jj,pp=0;
	int n,k;
	int ie,je,pom,sb=0;
	cin >> n >> k;
	for(i=1;i<=n;i++)
	for(j=1;j<=n;j++)
	cin >> polje[i][j];
	for(i=1;i<=k;i++)
	sorta[i]=i;
	for(i=1;i<=n;i++)
	for(j=1;j<=n;j++)
	{
		for(m=1;m<=k;m++)
		{
			if(sorta[m]==polje[i][j])
			{
				ii=i;
				jj=j;
				while(ie!=0 || je!=0 && provera==1)
				{
					if(ii==n)
					ie=0;
					if(jj==n)
					je=0;
					if(polje[ii+ie][jj]==polje[ii][jj+je])
					if(polje[ii+ie][jj]==sorta[m])
					{
						for(int z=ii+ie;z<=n;z++)
						for(int q=jj+je;j<=n;j++)
						if(polje[z][q]!=polje[ii][jj])
						{
							provera=0;
							z=n+1;
							q=n+1;
						}
					}
				}
				if(provera==1)
				{
					if(sb==0)
					{
					success[sb]=polje[i][j];
					sb++;
				}
					else{
						for(int xx=0;xx<sb;xx++)
						if(success[xx]==polje[i][j])
						pp++;
						if(pp==0)
						{
							success[sb]=polje[i][j];
							sb++;
						}
					}
				}
				else{
					for(int xx=0;xx<sb;xx++)
					{
						if(success[sb]==polje[i][j])
						{
							for(int ww=xx;ww<sb;ww++)
							success[ww]=success[ww+1];
							sb--;
						}
					}
				}
			}
		}
	}
	cout << sb;
}