#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll MOD = 1e9+7;
const int N = 2005;
const int M = 1e6+5;

int n,k,a[N][N],ver[N][N],hor[N][N];
int cnt[M];
bool vis[N][N],seen[M];

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>a[i][j];
            cnt[a[i][j]]++;
        }
    }

    for(int j=1;j<=n;j++){
        for(int i=n;i>=1;i--){
            if(a[i][j]==a[i+1][j])ver[i][j]=1+ver[i+1][j];
            else ver[i][j]=1;
        }
    }

    for(int i=1;i<=n;i++){
        for(int j=n;j>=1;j--){
            if(a[i][j]==a[i][j+1])hor[i][j]=1+hor[i][j+1];
            else hor[i][j]=1;
        }
    }

    int ans=0;

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(seen[a[i][j]])continue;
            seen[a[i][j]]=true;
            int c=hor[i][j];
            bool ok=true;
            for(int k=i;k<=i+ver[i][j]-1;k++){
                if(hor[k][j]!=c)ok=false;
            }
            if(ok){
                if(ver[i][j]*hor[i][j]==cnt[a[i][j]]){
                    ans++;
                }
            }
        }
    }

    cout<<ans;
    return 0;
}