#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
struct sorta
{
    int dx,lx,dy,ly,n;
};
using namespace std;
int m[2000][2000];
int n,k;
sorta x[1000005];
int poci,pocj,desno,dole;
void unos()
{
    int vred=0,d=0;
    cin>>n>>k;
    for(int i=0;i<=1000000;i++)
    {
        x[i].n=0;
        x[i].dx=0;
        x[i].dy=0;
        x[i].lx=n;
        x[i].ly=n;
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        {
            cin>>m[i][j];
            vred=m[i][j];
            x[vred].n++;
            x[vred].dx=max(x[vred].dx,j);
            x[vred].dy=max(x[vred].dy,i);
            x[vred].lx=min(x[vred].lx,j);
            x[vred].ly=min(x[vred].ly,i);
        }
    for(int i=1;i<=k;i++)
    {
        //cout<<x[i].ly<<" "<<x[i].lx<<"   "<<x[i].dy<<" "<<x[i].dx<<"   "<<x[i].n<<endl;
        if((x[i].dx-x[i].lx+1)*(x[i].dy-x[i].ly+1)==x[i].n)
        {
            d++;
        }
    }
    cout<<d;
}
int main()
{
    unos();
}