#include<cstdio>
#include<vector>
#define maxx 1000005
using namespace std;
int n,k,m;
int cnt[maxx],u[maxx],d[maxx],l[maxx],r[maxx],res;

void popuni() {
    for (int i=1;i<=maxx;i++) u[i]=maxx;
    for (int i=1;i<=maxx;i++) l[i]=maxx;
}

int main() {
    popuni();

    scanf("%d %d", &n, &k);
    for (int i=1;i<=n;i++) {
        for (int j=1;j<=n;j++) {
            scanf("%d", &m);
            cnt[m]++;
            u[m]=min(u[m],i);
            l[m]=min(l[m],j);
            d[m]=max(d[m],i);
            r[m]=max(r[m],j);
        }
    }

    for (int i=1;i<=k;i++) {
        if (!cnt[i]) continue;

        if ((d[i]-u[i]+1)*(r[i]-l[i]+1)==cnt[i]) res++;
    }
    printf("%d\n", res);

    return 0;
}