#include<bits/stdc++.h>
using namespace std;
int u,d,l,r,N,K,z,sorti[1000005],vocnjak[2005][2005];
bool prosli[1000005],nastaviti;
int main() {
    cin>>N>>K;
    for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++) {
            cin>>vocnjak[i][j];
            sorti[vocnjak[i][j]]++;
        }
    for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++) {
            if(prosli[vocnjak[i][j]]==false) {
                nastaviti=true;
                u=i;
                d=i;
                l=j;
                r=j;
               // cout<<i<<" je i, a j je:"<<j<<endl;
                while(u>0 and vocnjak[u][j]==vocnjak[i][j])
                    u--;
                u++;
                while(d<=N and vocnjak[d][j]==vocnjak[i][j])
                    d++;
                d--;
                while(l>0 and vocnjak[i][l]==vocnjak[i][j])
                    l--;
                l++;
                while(u>0 and vocnjak[i][r]==vocnjak[i][j])
                    r++;
                r--;
                //cout<<"pokazivaci: gore:"<<u<<" dole:"<<d<<" levo:"<<l<<" desno:"<<r<<endl;
                if((d-u+1)*(r-l+1)!=sorti[vocnjak[i][j]]) {
                    nastaviti=false;
                }
                else {
                    for(int k=u;k<=d;k++) {
                        if(!nastaviti)
                            break;
                        for(int p=l;p<=r;p++) {
                            if(vocnjak[k][p]!=vocnjak[i][j]) {
                                nastaviti=false;
                                break;
                            }
                    }
                    }
                }
                if(nastaviti)
                    z++;
                prosli[vocnjak[i][j]]=true;
       }
    }
    cout<<z<<endl;
}
/*
3 2
1 1 1
2 2 1
2 2 1



*/