#include <iostream>

using namespace std;

int main()
{
    int n,a,k;
    cin >> n >> k;
    int leva[400005];
    int desna[400005];
    int gornja[400005];
    int donja[400005];
    int brojac[400005];
    for(int i=0;i<k;i++)
    {
        leva[i]=n;
        gornja[i]=n;
        brojac[i]=0;
        donja[i]=-1;
        desna[i]=-1;
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin >> a;
            leva[a] = min(leva[a],j);
            desna[a] = max(desna[a],j);
            gornja[a]=min(gornja[a],i);
            donja[a]=max(donja[a],i);
            brojac[a]++;
        }
    }
    int s=0;
    for(int i=0;i<k;i++)
    {
        if(brojac[i]>0)
        {
            if((desna[i]-leva[i]+1)*(donja[i]-gornja[i]+1)==brojac[i]) s++;
        }
    }
    cout << s;
}