#include <bits/stdc++.h>
#define debug(x) cout << #x << " is " << x << '\n';
#define stigao() cout << "Stigao" << '\n';
#define file() freopen("output.txt", "w", stdout);

#define x first
#define y second

using namespace std;

typedef long long ll;

const int MAXK = 1e6 + 2;
vector< pair<int, int> > cord[MAXK];

bool isRect(int i){
    int maxX = 0, maxY = 0, minX = MAXK, minY = MAXK;
    for(auto p : cord[i]){
        maxX = max(p.x, maxX);
        maxY = max(p.y, maxY);
        minX = min(p.x, minX);
        minY = min(p.y, minY);
    }
    return (cord[i].size() == (maxX - minX + 1) * (ll)(maxY - minY + 1));
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n, k, t;
    cin >> n >> k;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            cin >> t;
            cord[t].push_back({i, j});
        }
    }
    ll cnt = 0;
    for(int i = 1; i <= k; i++){
        if(isRect(i))
            cnt++;
    }
    cout << cnt;
    return 0;
}