#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);

    // Input

    int len, k;
    cin >> len >> k;
    int num[len][len];

    set<int> vals;

    for (int y = 0; y < len; y++) {
        for (int x = 0; x < len; x++) {
            int n;
            cin >> n;
            vals.insert(vals.end(), n);
            num[x][y] = n;
        }
    }

    vector<int> values;
    values.assign(vals.begin(), vals.end());

    // wtf

    int counter = 0;

    for (int i = 0; i < values.size(); i++) {
        int u = -1, l = -1, d = -1, r = -1;

        // Define rect borders

        for (int y = 0; y < len; y++) {
            for (int x = 0; x < len; x++) {
                int n = num[x][y];

                if (n == values[i]) {
                    // Set Top-left coord

                    if (u == -1 && l == -1) {
                        l = x;
                        u = y;
                    }

                    // Set Bottom-right coord

                    if (x > r && x >= l)
                        r = x;
                    if (y > d && y >= u)
                        d = y;
                }

            }
        }

        // Look for non-good rects

        for (int y = 0; y < len; y++) {
            for (int x = 0; x < len; x++) {
                int n = num[x][y];

                if (n == values[i]) {
                    // Outside top-left

                    if (u != -1 && l != -1 && (x < l || y < u)) {
                        //cout << values[i] << " outside top-left. ";
                        //printf("%i %i compared to %i %i\n", x, y, l, u);
                        goto _skip;
                    }

                    // Outside bottom-right

                    if (d != -1 && r != -1 && (x > r || y > d)) {
                        //cout << values[i] << " outside bottom-right. ";
                        //printf("%i %i compared to %i %i\n", x, y, r, d);
                        goto _skip;
                    }
                }

                if (n != values[i]) {
                    if (u != -1 && l != -1 && d != -1 && r != -1 && x >= l && x <= r && y >= u && y <= d) {
                        //cout << values[i] << " ruins non-belonging square.\n";
                        goto _skip;
                    }
                }
            }
        }
        //cout << values[i] << ": " << l << ", " << u << ", " << r << ", " << d << endl;
        counter++;
        //cout << values[i] << endl;
        _skip:;
    }

    cout << counter;

    return 0;
}