#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 2005,K = 1e6+19;
ll mat[N][N],cnt[K],maxr[K],minr[K],maxc[K],minc[K];
int main(){
    ios_base::sync_with_stdio(false);
    ll sol = 0;
    for(long i = 1; i <= 1e6; i++){
        minr[i] = minc[i] = N;
    }
    ll n,k;
    cin >> n >> k;
    for(ll i = 1; i <= n; i++){
        for(ll j = 1; j <= n; j++){
            cin >> mat[i][j];
            ll x = mat[i][j];
            cnt[x]++;
            maxr[x] = max(maxr[x],i);
            minr[x] = min(minr[x],i);
            maxc[x] = max(maxc[x],j);
            minc[x] = min(minc[x],j);
        }
    }
    for(long i = 1; i <= k; i++){
        ll treba = (maxr[i]-minr[i]+1)*(maxc[i]-minc[i]+1);
        if(treba == cnt[i]){
            sol++;
        }
    }
    cout << sol << endl;
}