#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
    int n,k;
    scanf("%d %d",&n,&k);
    int matr[n][n];
    int predjeno[n][n];
    int pojave[k];
    for(int i = 0;i < n;i++)
        for(int j = 0;j < n;j++)
            predjeno[i][j] = 0;
    for(int i = 0;i < n;i++)
        for(int j = 0;j < n;j++)
            scanf("%d",&matr[i][j]);
    for(int i = 0;i < k;i++)
        pojave[i] = 0;
    for(int i = 0;i < n;i++)
        for(int j = 0;j < n;j++)
        {
            if(pojave[matr[i][j]]==0)
            {
                pojave[matr[i][j]] = 1;
                int donjakoordinata = i;
                int ky = -1;
                int kx = 0;
                do
                {
                    ky += 1;
                    predjeno[i+ky][j] = matr[i][j];
                    donjakoordinata = i+ky;
                }while(i+ky+1<n&&matr[i+ky][j]==matr[i+ky+1][j]);
                int p = 0;
                while(j+kx+1<n&&matr[i][j+kx]==matr[i][j+kx+1]&&p!=1)
                {
                    int donjakoordinata2 = i;
                    kx += 1;
                    ky = -1;
                    do
                    {
                        ky += 1;
                        donjakoordinata2 = i+ky;
                        predjeno[i+ky][j+kx] = matr[i][j];
                    }while(i+ky+1<n&&matr[i+ky][j+kx]==matr[i+ky+1][j+kx]);
                    if(donjakoordinata!=donjakoordinata2)
                    {
                        p = 1;
                        pojave[matr[i][j]] = 2;
                    }
                }
            }
            else if(predjeno[i][j]!=matr[i][j]){
                predjeno[i][j] = -matr[i][j];
                pojave[matr[i][j]] = 2;
            }
        }
        int suma = 0;
        for(int i = 0;i < k;i++)
            if(pojave[i]==1)
                suma++;
        printf("%d",suma);
    return 0;
}