#include <bits/stdc++.h>

using namespace std;

const int N = 2005;
const int K = 1e6 + 5;

int m[N][N], cnt[K];
bool a[K];
pair<int, int> f[K], l[K];


int main() {
    // hoodlum548<3
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr), cerr.tie(nullptr);
    int n, k;
    cin >> n >> k;
    for (int i = 0; i<n; ++i) {
        for (int j = 0; j<n; ++j) {
            cin >> m[i][j];
            if (!a[m[i][j]]) {
                f[m[i][j]] = {i, j};
                a[m[i][j]] = true;
            }
            l[m[i][j]] = {i, j};
            ++cnt[m[i][j]];
        }
    }

    int result = 0;

    for (int i = 1; i<k; ++i) {
        int need = (l[i].first - f[i].first + 1) * (l[i].second - f[i].second + 1);
        if (need == cnt[i]) {
           result++;
        }
    }
    cout << result << endl;
    return 0;
}