#include <iostream>
unsigned n,k,w=0;

void ek(unsigned a[],bool p[],unsigned ok,unsigned retard,unsigned ka[]){
    unsigned x=0,y=0;
    while(a[ok*n+retard+x]==a[ok*n+retard])x++;
    while(a[(ok+y)*n+retard]==a[ok*n+retard])y++;
    if(x*y!=ka[a[ok*n+retard]-1]) p[a[ok*n+retard]-1]=0;
    else{
        bool fuk=1;
        for(unsigned i=ok;i<ok+y;i++){
            for(unsigned j=retard;j<retard+x;j++)
                if(a[i*n+j]!=a[ok*n+retard]){fuk=0;p[a[ok*n+retard]-1]=0;break;}
            if(!fuk)break;
        }
        if(fuk){p[a[ok*n+retard]-1]=0;w++;}
    }
}

int main(){
    std::cin>>n>>k;
    unsigned a[n][n];
    unsigned ka[k];
    bool p[k];
    for(unsigned i=0;i<k;i++){
        p[i]=1;
        ka[i]=0;
    }
    for(unsigned i=0;i<n;i++)
        for(unsigned j=0;j<n;j++){
            std::scanf("%d",&a[i][j]);
            ka[a[i][j]-1]++;
        }
    for(unsigned i=0;i<n;i++)
        for(unsigned j=0;j<n;j++)
            if(p[a[i][j]-1])
                ek(a[0],p,i,j,ka);
    std::cout<<w;
}