#include <iostream>

using namespace std;

long long int u[10000000];

int main()
{
    long long int n, k, p = 0, y = 0, t = 0, maxg = -1, maxh = -1;

    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];
        }
    for(int i = 0; i < n; ++i)
    {
        for(int j = 0; j < n; ++j)
        {
            maxg = 0;
            maxh = 0;
            t = 0;
            y = 0;
            if(!u[a[i][j]])
            {
                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;
                                        goto label;
                                    }
                        }
                    }
                }
                label:
                if(y)
                    --p;
            }
            u[a[i][j]] = 1;
        }
    }
    for(int i = 1; i <= k; ++i)
    {
        if(u[i] == 1)
            ++p;
    }
    cout << p << endl;
    return 0;
}