#include <iostream>
#include <stdio.h>
#define MAX 1000000
using namespace std;
int n,k,mat[2001][2001],niz[MAX],maxX[MAX],maxY[MAX],minY[MAX],minX[MAX];
int main()
{
    int brojac=0;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            scanf("%d",&mat[i][j]);
            niz[mat[i][j]]++;
            int a=mat[i][j];
            if(maxX[mat[i][j]]==0)maxX[a]=i;/**X=I*/
            if(maxY[mat[i][j]]==0)maxY[a]=j;/**Y=J*/
            if(minX[mat[i][j]]==0)minX[a]=i;
            if(minY[mat[i][j]]==0)minY[a]=j;

            if(maxX[mat[i][j]]<i)maxX[mat[i][j]]=i;
            if(maxY[mat[i][j]]<j)maxY[mat[i][j]]=j;
            if(minX[mat[i][j]]>i)minX[mat[i][j]]=i;
            if(minY[mat[i][j]]>j)minY[mat[i][j]]=j;
        }
    }
    
    for(int i=1;i<=k;i++)
        if(!maxX[k])
            if((maxX[i]-minX[i]+1)*(maxY[i]-minY[i]+1)==niz[i])
                brojac++;

   printf("%d",brojac);
    return 0;
}