#include <iostream>
using namespace std;
int main()
{
    short n;
    long k, resenje = 0;
    cin >> n >> k;
    long m[n][n];
    for(short i = 0; i < n; i++)
        for(short j = 0; j < n; j++)
            cin >> m[i][j];
    bool a[k] = {false}, provera;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            if(!a[m[i][j] - 1]) {
                const short y = j;
                short x = i;
                while(j < n) {
                    if(m[i][j + 1] != m[i][y])
                        break;
                    j++;
                }
                while(x < n) {
                    if(m[x + 1][j] != m[i][y])
                        break;
                    x++;
                }
                a[m[i][y] - 1] = true;
                provera = false;
                if(((x - i) != (j - y)) || (x == i)) {
                    for(short q = 0; q < n; q++) {
                        for(short w = 0; w < n; w++) {
                            if(((q < i || q > x || w < y || w > j) && (m[q][w] == m[i][y])) || ((q >= i && q <= x && w >= y && w <= j) && (m[q][w] != m[i][y]))){
                                provera = true;
                                break;
                            }
                        }
                        if(provera)
                            break;
                    }
                    if(!provera) {
                        resenje++;
                    }
                }
            }
        }
    }
    cout << resenje;
    return 0;
}