#include <bits/stdc++.h>
using namespace std;
//#define si short int
#define ll long long
#define maxn 2005
/*
ll a[maxn][maxn];
vector<vector<pair<ll,ll>>> ima;


int main(void)
{
    ll n,k;
    cin>>n>>k;
    ima.resize(k);
    for(ll i=0;i<n;i++)
        for(ll j=0;j<n;j++)
            cin>>a[i][j], ima[a[i][j]].push_back({i,j});
    //sort(ima.begin(),ima.end());  pravi problem, ne sortira lepo

    for(ll i=1;i<=k;i++) //U ovom foru trebam da ispitam da li formira pravugaonik i da li je samo na jednom mestu
    {
        if(ima[i].size()!=0)
        {

            cout<<a[ima[i][0].first][ima[i][0].second]<<endl;
            for(ll j=0;j<ima[i].size();j++)
                cout<<ima[i][j].first<<' '<<ima[i][j].second<<endl;
            cout<<endl;


        }
    }
}
*/

ll a[2005][2005];
vector<ll> ks;
vector<ll> dl;
bool posecen[1000005]={false};
//ll posecen[1000005]={0};
ll ima[1000005]={0};
ll koliko_ima=0;
ll n,k;
void popuni(ll i, ll j, ll x);

int main(void)
{
    ll n,k;
    scanf("%lld %lld",&n,&k);
    ks.resize(k,0);
    for(ll i=0;i<n;i++)
        for(ll j=0;j<n;j++)
            {
                cin>>a[i][j];
                ima[a[i][j]]++;
                if(ks[a[i][j]]==0) {ks[a[i][j]]=1; koliko_ima++;}
            }

    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<n;j++)
        {
            //if(posecen[a[i][j]]!=0 && posecen[a[i][j]]!=ima[a[i][j]] && find(dl.begin(), a[i][j], dl.end())!=dl.end()) dl.push_back(a[i][j]);
            if(posecen[a[i][j]]==false)
                popuni(i,j,a[i][j]);
        }
    }
    //for(ll i=0;i<dl.size();i++)
        //cout<<dl[i]<<' ';
    printf("%lld",koliko_ima-dl.size());
}

void popuni(ll i, ll j, ll x)
{
    posecen[x]=true;
    //ll mini, ll maxi;
    ll t=i, y=i, o=j, u=j;
    while(a[t][j]==x) t++;
    t--;
    while(a[y][j]==x) y--;
    y++;
    while(a[i][u]==x) u++;
    u--;
    while(a[i][o]==x) o--;
    o++;

    ll dime=(t-y+1)*(u-o+1);
    //cout<<dime<<endl;
    for(;y<=t;y++)
        for(;o<=u;o++)
        {
            if(a[y][o]!=x)
            {
                //cout<<"1"<<endl;
                dl.push_back(x);
                return;
            }
        }
    if(ima[x]!=dime)
    {
        //cout<<"2"<<endl;
        //cout<<dime<<' '<<ima[x]<<endl;
        dl.push_back(x);
        //cout<<endl<<brcs<<endl;
        return;
    }
}