#include <iostream>

using namespace std;

int K[2000][2000];
int T[1000000];

check(int i, int j, int q, int p, int k)
{
    /*cout << i << ' ' << j << endl;
    cout << q << ' ' << p << endl;
    cout << endl;*/
    for(i; i < q-1; i++)
        for(j; j < p-1; j++)
            if(K[i][j] != k)
            {
                return false;
            }

    return true;
}

bool finde(int k, int n, int r)
{
    int l = 0;
    int i = 0;
    int j = 0;
    int vrsta = 0;
    int kolona = 0;
    while(l != k && i <= n)
    {
        j = 0;
        while(l != k && j <= n)
        {
            l = K[i][j];
            j++;
        }
        i++;
    }

    /*for(i = 0; i < n; i++)
        for(j = 0; j < n; j++)
            if(K[i][j] == k)
                break;
    cout << i-1 << " " << j-1 << endl;*/

    int q = i, p = j;
    j--;
    i--;

    while(l == k && p <= n)
    {
        l = K[i][p];
        p++;
        vrsta++;
    }

    l = K[i][j];

    while(l == k && q <= n)
    {
        l = K[q][j];
        q++;
        kolona++;
    }

    q -= i;
    p -= j;

    //cout << vrsta*kolona << ' ' << r << endl;

    if(r != 0 && r == vrsta*kolona && check(i, j, q+i, p+j, k))
        return true;

    return false;

}

int main()
{
    int n, k, r = 0;

    cin >> n >> k;

    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
        {
            cin >> K[i][j];
            T[K[i][j]-1]++;
        }

    for(int i = 1; i < k; i++)
        if(finde(i, n, T[i-1]))
            r++;

    cout << r;

    return 0;
}