using System;
    class Program
    {
        static int odradi(int a,int b,long k)
        {
            int i = a;
            int j = b;

            while (mat[i, b] == k)
            {
                i++;
                if (i >= n)
                    break;
            }
                
            
            while (mat[a, j] == k)
            {
                j++;
                if (j >= n)
                    break;

            }

            int razlika = j - b;
            if ((i-a)*(j-b)!=niz[k-1])
                return 0;
            
            for (; a < i; a++)
                for (; b < j; b++)
                    if (mat[a, b] != k)
                        return 0;
            pogodni++;
            return razlika;
        }
        static int n;
        
        static long[] niz;
        static long[,] mat;
        static int pogodni = 0;
        static void Main(string[] args)
        {
            long k;
            string[] t = Console.ReadLine().Split();
            
            n = int.Parse(t[0]);
            k = long.Parse(t[1]);
             niz = new long[k];
            bool[] niz2 = new bool[k];
             mat = new long[n, n];
            for (int i = 0; i < n; i++)
            {
                t = Console.ReadLine().Split();
                for (int j = 0; j < n; j++)
                {
                    niz[(mat[i, j] = long.Parse(t[j])) - 1]++;
                }
            }
           // for (int i = 0; i < k; i++)
            {
                // if (niz[i] % 2 == 0)
                for (int j = 0; j < n; j++)
                    for (int p = 0; p < n; p++)
                        if (!niz2[mat[j, p]])
                        {
                            niz2[mat[j, p]] = true;
                            p += odradi(j, p, mat[j, p]);
                        }
                Console.Write(pogodni);
               
            }

        }
    }