using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BDobri_pravougaonici
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] u = Console.ReadLine().Split(' ');
            int n = int.Parse(u[0]);
            int k = int.Parse(u[1]);
            int[,] m = new int[n, n];
            int[] minI = new int[k + 1];
            int[] minJ = new int[k + 1];
            for (int i = 1; i <= k; i++)
            {
                minI[i] = int.MaxValue;
                minJ[i] = int.MaxValue;
            }
            int[] maxI = new int[k + 1];
            int[] maxJ = new int[k + 1];
            int[] a = new int[k + 1];
            int br = 0;
            for (int i = 0; i < n; i++)
            {
                string[] r = Console.ReadLine().Split(' ');
                for (int j = 0; j < n; j++)
                {
                    m[i, j] = int.Parse(r[j]);
                    int x = m[i, j];
                    a[x]++;
                    minI[x] = Math.Min(minI[x], i);
                    minJ[x] = Math.Min(minJ[x], j);
                    maxI[x] = Math.Max(maxI[x], i);
                    maxJ[x] = Math.Max(maxJ[x], j);
                }
            }
            for (int x = 1; x <= k; x++)
            {
                if ((maxI[x] - minI[x] + 1) * (maxJ[x] - minJ[x] + 1) == a[x]) br++;
            }
            Console.WriteLine(br);
        }
    }
}