#include<bits/stdc++.h>
using namespace std;
#define ll long long
int lista[3000][3000];
int X[1000100],x[1000100],y[1000100],Y[1000100];
map<int,int> m;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n,k;
    cin >> n >> k;
    for(int i=0;i<1000010;i++){
        X[i]=0;
        x[i]=INT_MAX;
        y[i]=INT_MAX;
        Y[i]=0;
    }

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin >> lista[i][j];
            m[lista[i][j]]++;
            X[lista[i][j]] = max(X[lista[i][j]],j);
            x[lista[i][j]] = min(x[lista[i][j]],j);
            Y[lista[i][j]] = max(Y[lista[i][j]],i);
            y[lista[i][j]] = min(y[lista[i][j]],i);
        }
    }
    ll resenje=0;
    for(int i=1;i<=k;i++){
        if((X[i]-x[i]+1)*(Y[i]-y[i]+1) == m[i]) resenje++;
    }
    cout << resenje;

    return 0 ;
}