#include <bits/stdc++.h>

using namespace std;
#define MAXN 1005
#define MAXK 1000005
#define INF 1000000000
typedef pair<int,int> pii;
vector <pii> pos[MAXK];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n,k;
    cin >> n >> k;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            int x;
            cin >> x;
            pos[x].push_back({i,j});
        }
    }
    int ans=0;
    for(int i = 1; i <= k; i++)
    {
        int minx = INF;
        int miny = INF;
        int maxx = 0;
        int maxy = 0;
        if(pos[i].size()==0)continue;
        for(auto x:pos[i])
        {
            minx=min(x.first,minx);
            maxx=max(x.first,maxx);
            miny=min(x.second,miny);
            maxy=max(x.second,maxy);
        }
        if((maxx-minx+1)*(maxy-miny+1)==pos[i].size())ans++;
    }
    cout << ans;
    return 0;
}