#include <iostream>

using namespace std;

int main()
{
    int N, K;
    int i, j, prov,s;
    cin >> N >> K;
    int matrica[N][N];
    int niz[K];
    for(i = 0; i < K; i++)
        niz[i] = 0;
    for(i = 0 ; i < N; i++){
        for(j = 0 ; j < N; j++){
            cin >> matrica[i][j];
            niz[matrica[i][j] - 1]++;
        }
    }
    int j1, i1, r;
    for(i = 0; i < N; i++){
        for(j = 0; j < N; j++){
            if(niz[matrica[i][j] - 1] == -1){
                matrica[i][j] = -1;
            }
            if(matrica[i][j] == -1 || matrica[i][j] == 0) continue;
            prov = matrica[i][j];
            r = 0;
            matrica[i][j] = 0;
            r++;
            j1 = j + 1;
            i1 = i + 1;
            //cout << matrica[i][j1];
            while(j1 < N && prov == matrica[i][j1]){
                matrica[i][j1] = 0;
                j1++;
                r++;
            }
            s = j;
            while(i1 < N && prov == matrica[i1][s]){
                    matrica[i1][s] = 0;
                    r++;
                    if(s + 1 == j1){
                    i1++;
                    s = j;
                    }
                    else s++;
            }
            if((r != (j1 - j)*(i1 - i)) || ( r != niz[prov - 1])){
                niz[prov-1] = -1;

            }
        }
    }
    int resenje = 0;
    for(i = 0 ; i < K; i++){
        if(niz[i] > 0)
            resenje++;
    }
   /*for(i = 0 ; i < N; i++){
        for(j = 0 ; j < N; j++){
            cout << matrica[i][j];
            //niz[matrica[i][j] - 1]++;
        }
        cout<< endl;
    }*/
    cout << resenje;
    return 0;
}