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

struct koordinate
{
    int x1;
    int y1;
    int x2;
    int y2;
};

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n,k,g,br=0,r;
    cin >> n >> k;
    vector<koordinate> v(k+1);
    vector<int> brojBrojeva(k+1,0);
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<n;j++)
        {
            cin >> g;
            if(brojBrojeva[g]==0)
            {
                v[g].x1=n+1;
                v[g].y1=n+1;
                v[g].x2=0;
                v[g].y2=0;
            }
            v[g].x1=min(i,v[g].x1);
            v[g].x2=max(i,v[g].x2);
            v[g].y1=min(j,v[g].y1);
            v[g].y2=max(j,v[g].y2);
            brojBrojeva[g]++;
        }
    }
    for (int i=1;i<=k;i++)
    {
        r=(v[i].x2-v[i].x1+1)*(v[i].y2-v[i].y1+1);
        if(r==brojBrojeva[i])
            br++;
    }
    cout << br;
}