#include <iostream>

using namespace std;

int main()
{
    int n, b = 0;
    cin >> n;
    int m[n][n];
    int k;
    cin >> k;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> m[i][j];
        }
    }
    for (int l = 1; l <= k; l++) {
        char c = true;
        char d = false;
        int lg = 0, gg = 0, b1 = 0, b2 = 0, dg = n, ng = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (b2 == 1 && j >= lg && j <= dg && i >= gg && i <= ng)
                    continue;
                if (m[i][j] == l && b1 == 1) {
                    c = false;
                    break;
                }
                if (m[i][j] == l) {
                    d = true;
                    b1++;
                    lg = j;
                    gg = i;
                    ng = i;
                    for (;ng < n; ng ++) {
                        if (m[ng][lg] != l)
                        break;
                        for (int o = lg; o <= dg; o++) {
                            if ((o == dg || m[ng][o] != l) && b2 == 0 ) {
                                dg = o - 1;
                                b2++;
                                break;
                            }
                            if (b2 == 1 && m[ng][o] != l) {
                                c = false;
                                break;
                            }
                        }
                    }
                    ng--;
                }
            }
        if (!c)
            break;
        }
        if (c && d)
            b++;
    }
    cout << b;

    return 0;
}