#include <iostream>

using namespace std;

int main()
{
    int n,a,k;
    cin >> n >> k;
    int leva[1000005], desna[1000005], gornja[1000005], donja[1000005], brojac[1000005];
    for(int i=0;i<k;i++)
    {
        leva[i]=gornja[i]=brojac[i]=0;
        donja[i]=desna[i]=n;
    }
    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])*(donja[i]-gornja[i])==brojac) s++;
        }
    }
    cout << s;
}