#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

int ima[1000005];
int gore[1000005];
int levo[1000005];
int desno[1000005];
int dole[1000005];

int main(){
    ios_base::sync_with_stdio(false);
    cout.precision(10);
    cout<<fixed;

    int n, k;
    cin >> n >> k;
    for(int i=1; i<=k; i++){
        gore[i] = n+1;
        levo[i] = n+1;
    }
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            int x;
            cin >> x;
            gore[x] = min(gore[x], i);
            dole[x] = max(dole[x], i);
            levo[x] = min(levo[x], j);
            desno[x] = max(desno[x], j);
            ima[x]++;
        }
    }
    int cnt=0;
    for(int i=1; i<=k; i++){
        if(ima[i] == (1+dole[i]-gore[i])*(1+desno[i]-levo[i])) cnt++;
    }
    cout << cnt;
    return 0;
}