#include <bits/stdc++.h>

using namespace std;

int a[2001][2001];
vector<pair<int, int>> b[(int)1e6+1];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    set<int> nums;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            cin >> a[i][j];
            nums.insert(a[i][j]);
            b[a[i][j]].push_back({i, j});
        }
    }

    long long rez = 0;

    for(auto i:nums)
    {
        int minx = 3000, miny = 3000, maxx = 0, maxy = 0;
        bool moze = true;
        for(int j = 0; j < b[i].size(); j++)
        {
            minx = min(minx, b[i][j].first);
            miny = min(miny, b[i][j].second);
            maxx = max(maxx, b[i][j].first);
            maxy = max(maxy, b[i][j].second);
        }

        if((maxx-minx+1)*(maxy-miny+1) != b[i].size())
            continue;

        for(int j = 0; j < b[i].size(); j++)
        {
            if(b[i][j].first < minx || b[i][j].first > maxx || b[i][j].second < miny || b[i][j].second > maxy)
                moze = false;
        }

        if(moze)
            rez++;
    }

    cout << rez;

}