#include <bits/stdc++.h>
#define mod 1000000007
#define pb push_back
#define mid(l, r) ((l)+(r))/2
#define len(a) (a).length()
#define sz(a) (a).size()
#define xx first
#define yy second
#define inf int(2e9)
#define ff(i, a, b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i, a, b) for(int (i) = (a); (i) >= (b); --(i))


using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

template<class T>
void print(const T niz[], const int siz)
{
    for(int i=0;i<siz;i++)
        cout << niz[i] << " ";
    cout << endl;
}

int n,k;
int mat[2005][2005];
vector<pii>boje[1000005];
int main()
{
    ios_base::sync_with_stdio(false);
    cin >> n >> k;
    ff(i,0,n-1){
        ff(j,0,n-1){
            cin >> mat[i][j];
            boje[mat[i][j]].pb({i,j});
        }
    }
    ll res = 0;
    ff(i,1,k){
        if(boje[i].size() <= 1){
            if(boje[i].size() == 1){
                res++;
            }
            continue;
        }
        pii maks = {0,0};
        pii mini = {1e9,1e9};
        for(auto c:boje[i]){
            maks = max(maks, c);
            mini = min(mini, c);
        }
        int vel = (maks.xx - mini.xx + 1) * (maks.yy - mini.yy + 1);
        bool dobar = 1;
        if(vel != boje[i].size())
            continue;
        for(auto c:boje[i]){
            if(c.xx > maks.xx || c.xx < mini.xx){
                dobar = 0;
            }
            if(c.yy > maks.yy || c.yy < mini.yy){
                dobar = 0;
            }
        }
        if(dobar)
            res++;
    }
    cout << res << "\n";
    return 0;
}