#include <iostream>
#include <stdlib.h>

using namespace std;
const int N=1e5+10;

int main()
{
    long br=0,i,j,n,k;
    cin >> n >> k;
    long* x1=(long*)malloc(sizeof(long)*(k+1));
    long* y1=(long*)malloc(sizeof(long)*(k+1));
    long* x2=(long*)malloc(sizeof(long)*(k+1));
    long* y2=(long*)malloc(sizeof(long)*(k+1));
    long* y_mn=(long*)malloc(sizeof(long)*(k+1));
    long* y_mx=(long*)malloc(sizeof(long)*(k+1));
    long* b=(long*)malloc(sizeof(long)*(k+1));
    long* p=(long*)malloc(sizeof(long)*(k+1));
    for (i=1;i<=k;i++)
    {
        b[i]=0;
        p[i]=0;
    }
    for (i=1;i<=n;i++)
    {
        for (j=1;j<=n;j++)
        {
            long d;
            cin >> d;
            if (b[d]==0) {x1[d]=i; y1[d]=j; b[d]=1;
            /*x_mn[d]=i;*/ y_mn[d]=j; /*x_mx[d]=i;*/ y_mx[d]=j;}
            else
            {
                //if (i<x_mn[d]) x_mn[d]=i;
                if (j<y_mn[d]) y_mn[d]=j;
                //if (i>x_mx[d]) x_mx[d]=i;
                if (j>y_mx[d]) y_mx[d]=j;
            }
            x2[d]=i;
            y2[d]=j;
            ++p[d];
        }
    }
    for (i=1;i<=k;i++)
        if (b[i]!=0)
        {
            if (y1[i]==y_mn[i] && y2[i]==y_mx[i])
            if (p[i]==((y2[i]-y1[i]+1)*(x2[i]-x1[i]+1))) ++br;
            /*cout << y1[i] << ' ' << y2[i] << ' ' << y_mn[i] << ' ' << y_mx[i] << "\n";*/
            /*cout << p[i] << ' ' << x1[i] << ' ' << x2[i] << "\n";*/
        }
    cout << br;
    return 0;
}