#include <iostream>

using namespace std;

int N = 1;

bool Pravougaonik(long matrica[N][N], int i, int j)
{
    int iMin = i;
    int jMin = j;
    int iMax = N - 1;
    int jMax = N - 1;

    ZadnjeIJ(matrica, iMin, jMin, iMax, jMax)

    if(iMax == iMin && jMax == jMin)
        return true;

    for(; iMin <= iMax; iMin++)
        for(; jMin <= jMax; jMin++)
            if(matrica[i][j] != matrica[iMin][jMin])
                return false;

    return true;
}

void ZadnjeIJ(long matrica[][N], int iMin, int jMin)
{
    int i = N - 1;
    int j = N - 1;
    bool tf = false;
    for(; i > iMin && !tf; i--)
        for(; j > jMin && !tf; j--)
            if(matrica[i][j] == matrica[iMin][jMin])
                tf = true;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cerr.tie(0);

    long K;

    cin>>N>>K;

    bool poseceno[K];
    long matrica[N][N];
    long zbir = 0;

    for(int i = 0; i < N; i++)
        for(int j = 0; j < N; j++)
        {
            cin>>matrica[i][j];
            poseceno[i] = false;
        }

    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
            if(!poseceno[matrica[i][j]] && Pravougaonik(matrica, i, j))
            {
                poseceno[matrica[i][j]] = true;
                zbir ++;
            }
        }
    }

    cout<<zbir;

}