#include <iostream>

using namespace std;

int main()
{
    long long int n, k, p = 0, y = 0, maxg = -1, maxh = -1, u[100000];

    cin >> n;
    cin >> k;

    long long int a[n][n];

    for(int i = 0; i < n; ++i)
        for(int j = 0; j < n; ++j)
        {
            cin >> a[i][j];
            u[a[i][j]] = 1;
        }
    for(int i = 0; i < n; ++i)
    {
        for(int j = 0; j < n; ++j)
        {
            maxg = 0;
            maxh = 0;
            y = 0;
                for(int g = n-1; g >= 0; --g)
                {
                    for(int h = n-1; h >= 0; --h)
                    {
                        if(a[g][h] == a[i][j])
                        {
                            if(g > maxg)
                                maxg = g;
                            if(h > maxh)
                                maxh = h;
                        }
                    }
                }
                for(int d = i; d <= maxg; ++d)
                    for(int f = j; f <= maxh; ++f)
                        if(a[d][f] != a[i][j])
                        {
                            y = 1;
                        }
                if(y)
                    u[a[i][j]] = 0;
        }
    }
    for(int i = 1; i <= k; ++i)
    {
        if(u[i] == 1)
            ++p;
    }
    cout << p << endl;
    return 0;
}