#include <iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cmath>
using namespace std;

int n,k,mg[2004][2004],marka[2004][2004],r[1000006];

int main()
{
    int i,j,a,b,c,m,x,y,z,krx,kry,res=0,tv,f;

    cin>>n>>k;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++) cin>>mg[i][j];
    }

    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(marka[i][j]==1) continue;
            tv=mg[i][j];
            if(r[tv]==1) r[tv]=-1;

            for(x=j;x<=n;x++)
            { if(mg[i][x]!=tv) {x--; break;} }
            if(x==n+1) x=n;

            for(y=i;y<=n;y++)
            { if(mg[y][j]!=tv) {y--; break;}}
            if(y==n+1) y=n;

            krx=y;  kry=x;  f=0;

            //cout<<"tunj "<<i<<" "<<j<<" "<<krx<<" "<<kry<<endl;

            for(x=i;x<=krx;x++)
            {
                for(y=j;y<=kry;y++)
                {
                    marka[x][y]=1;
                    if(mg[x][y]!=tv) f=1;
                }
            }

            if(f==0 && r[tv]==0) {r[tv]=1;}
            else r[tv]=-1;
        }
    }


    res=0;
    for(i=1;i<=k;i++) if(r[i]==1) res++;

    cout<<res;

    return 0;
}