#include <iostream>

using namespace std;

int main()
{
    int N,K;
    cin >>N>>K;
    int matrica [N][N];
    int PogodneTeritorije =0;

    for (int i=0;i<N;i++)
        for (int j=0;j<N;j++)
            cin >>matrica [i][j];

    int BrojJabukaUSorti [K+1] = {0};

     for (int i=0;i<N;i++)
        for (int j=0;j<N;j++)
         BrojJabukaUSorti [matrica [i][j]] ++;
    bool PerineSorte [K+1] = {0};

    for (int x =0 ; x< N;x++)
    {
         for (int y =0 ; y < N ; y++)
         {
            if ( PerineSorte[matrica[x][y]]==0)
            {
                PerineSorte[matrica[x][y]]=1;
                int k = matrica [x][y];
                int sirina = 1;
                int duzina = 1;
                int j =0, i = 0;
                int brojkaova=0;
                while (matrica [x][y+i]==k)
                {
                    sirina++;
                    i++;
                }
                while (matrica [x+j][y]==k)
                {
                    duzina++;
                    j++;
                }
                sirina--;
                duzina--;
                for (int l=x;l<x+duzina;l++)
                {
                    for (int r=y;r<y+sirina;r++)
                    {

                    if (matrica [l][r]== k )
                        {
                        brojkaova++;
                        }
                    }

                }
                if (brojkaova==BrojJabukaUSorti[k])
                {
                    PogodneTeritorije++;
                }
            }
        }
    }
    cout << PogodneTeritorije;
    return 0;
}