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

#define pb push_back

int n, k;
int mat[2005][2005];
int howMany[1000005];
vector<pair<int, int>> where[1000005];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> k;

    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            cin >> mat[i][j];
            ++howMany[mat[i][j]];
            where[mat[i][j]].pb({i, j});
        }
    }

    int res = 0;
    for(int i = 1; i <= k; ++i){
        int i1 = 1e4, i2 = -1, j1 = 1e4, j2 = -1;

        for(auto pos : where[i]){
            i1 = min(i1, pos.first);
            i2 = max(i2, pos.first);
            j1 = min(j1, pos.second);
            j2 = max(j2, pos.second);
        }

        if((i2 - i1 + 1) * (j2 - j1 + 1) == howMany[i]){++res;}
    }

    cout << res << "\n";

    return 0;
}