#include<iostream>
#include<cstdlib>
#include<string>

//#define DEBUG

using namespace std;

long long X[1000001],Y[1000001],P[1000001],Q[1000001],T[1000001];
long long M[2001][2001], V[2001][2001];

int main()
{
    long long N,K;

    cin >> N >> K;

    long long M[N][N],V[N][N];

    long long r=0;

    for (int i=0;i<N;i++)
    {
        for (int j=0;j<N;j++)
        {
            cin >> M[i][j];
            V[i][j]=0;
        }
    }

    #ifdef DEBUG
    cout << endl << "=== DEBUG MODE ====================" << endl;

    for (int i=0;i<N;i++)
    {
        for (int j=0;j<N;j++)
        {
            cout << M[j][i] << "\t";
        }
        cout << endl;
    }

    #endif // DEBUG

    for (int i=0;i<N;i++)
    {
        for (int j=0;j<N;j++)
        {
            #ifdef DEBUG
            cout << endl;
            cout << "M[" << i << ", " << j << "]= " << M[i][j] << endl;
            #endif // DEBUG

            if (V[i][j] == 0)
            {
                #ifdef DEBUG
                cout << endl;
                cout << "-----------------------------------" << endl;
                cout << "Provera za (" << i << ", " << j << ")" << endl;
                cout << "-----------------------------------" << endl;
                #endif // DEBUG

                int a = M[i][j];

                #ifdef DEBUG
                cout << "Vrsta " << a << endl;
                #endif // DEBUG

                V[i][j]=1;

                if (T[a]==0)
                {
                    #ifdef DEBUG
                    cout << "Nova vrsta!" << endl;
                    #endif // DEBUG

                    X[a]=j;
                    Y[a]=i;

                    while (M[i][j]==a && j<N)
                    {
                        V[i][j]=1;
                        j++;
                    }
                    j--;

                    Q[a]=i;
                    P[a]=j;
                    T[a]=1;

                    #ifdef DEBUG
                    cout << "Vrsta " << a << " dimenzija:" << endl;
                    cout << "  X: " << X[a] << endl;
                    cout << "  Y: " << Y[a] << endl;
                    cout << "  P: " << P[a] << endl;
                    cout << "  Q: " << Q[a] << endl;
                    #endif // DEBUG
                }
                else if (T[a]==1)
                {
                    #ifdef DEBUG
                    cout << "Postojeca vrsta!" << endl;
                    #endif // DEBUG
                    if (j!=X[a])
                    {
                        T[a]=-1;
                        #ifdef DEBUG
                        cout << "Ne odgovara X koordinata: " << endl;
                        cout << "  " << X[a] << " != " << j << endl;
                        #endif // DEBUG
                        continue;
                    }
                    if (i-1!=Q[a] && i>0)
                    {
                        T[a]=-1;
                        #ifdef DEBUG
                        cout << "Ne odgovara Y koordinata: " << endl;
                        cout << "  " << Y[a] << " != " << i << endl;
                        #endif // DEBUG
                        continue;
                    }

                    while (M[i][j]==a && j<=P[a])
                    {
                        V[i][j]=1;
                        j++;
                    }
                    j--;

                    Q[a]++;

                    #ifdef DEBUG
                    cout << "Vrsta " << a << " dimenzija:" << endl;
                    cout << "  X: " << X[a] << endl;
                    cout << "  Y: " << Y[a] << endl;
                    cout << "  P: " << P[a] << endl;
                    cout << "  Q: " << Q[a] << endl;
                    #endif // DEBUG
                }
            }
        }
    }

    for (int i=1;i<=K;i++)
    {
        #ifdef DEBUG
        cout << T[i] << " ";
        #endif // DEBUG
        if (T[i]==1)
            ++r;
    }
    #ifdef DEBUG
    cout << endl;
    #endif // DEBUG
    cout << r;

    return 0;
}