#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define MAX1 2001
 using namespace std;

 struct a
{
    int mx,my,br;
    int mnx,mny;

};

 main()
{
    int n,i,j,v,k,uk=0;
     scanf("%d",&n);
     scanf("%d",&k);
     struct a u[100000];


    for (i=1;i<=k;i++)
    {
      u[i].mx=0;
      u[i].my=0;
      u[i].mnx=n+1;
      u[i].mny=n+1;
      u[i].br=0;
    }
    for (i=1;i<=n;i++)
        for(j=1;j<=n;j++)
    {
         scanf("%d",&v);
         u[v].br++;
         if(i>u[v].mx) u[v].mx=i;
         if(j>u[v].my) u[v].my=j;
         if(i<u[v].mnx) u[v].mnx=i;
         if(j<u[v].mny) u[v].mny=j;
     }
// for (i=1;i<=k;i++) printf("%d   %d  %d   %d   %d  %d\n",i, u[i].br, u[i].mnx,u[i].mny,u[i].mx,u[i].my);
    for (i=1;i<=k;i++)

       if ((u[i].mx>0) && (u[i].my>0) && (u[i].br==(u[i].mx-u[i].mnx+1)*(u[i].my-u[i].mny+1))) uk++;


     printf("%d",uk);
}