#include <bits/stdc++.h>

using namespace std;

int main(void)
{
    int n,k,d = 1,f = 1,lukica = 0,x = 1,frag,suma = 0;
    cin >> n >> k;
    int a[k];
    int matrica[n][n];
    for(int i = 0;i < k;i++){
        a[i] = 0;
    }

    for(int i = 0;i < n;i++){
        for(int j = 0;j < n;j++){
            cin >> matrica[i][j];
    }
    }
    for(int i = 0;i < n;i++){
        for(int j = 0;j < n;j++){
            if(matrica[i][j] != 0)
            {
                int p = matrica[i][j];
                matrica[i][j] = 0;
                while(matrica[i][j+d] == p){
                    matrica[i][j+d] = 0;
                    d++;
                }
                while(matrica[i+f][j] == p && x == 1){
                    lukica = 0;
                    x = 1;
                    frag = j+d;
                    for(int h = j;h < frag;h++){
                        if(matrica[i+f][h] == p){
                            matrica[i+f][h] = 0;
                            lukica++;
                        }
                    }
                        f++;

                    if(lukica != d){
                        x = 0;
                    }
                }
                if(x == 1){
                    a[p]++;
                }

            }

            d = 1;
            f = 1;
            lukica = 0;
            x = 1;




        }
    }
    for(int i = 0;i < k;i++){
        if(a[i] == 1)
            suma++;
    }
    cout << suma;

    return 0;
}