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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            bool start;
            bool uj;
            bool ui;
            bool longs;
            string[] s = Console.ReadLine().Split(' ');
            int N = Convert.ToInt32(s[0]);
            int K = Convert.ToInt32(s[1]);
            int[,] M = new int[N, N];
            string[] r;
            for (int i = 0; i < N; i++)
            {
                r = Console.ReadLine().Split(' ');
                for (int j = 0; j < N; j++)
                {
                    M[i, j] = Convert.ToInt32(r[j]);
                }
            }
            int indexj, indexi, loci, longj, mini, brj, any;
            int result = 0;
            for (int k = 1; k <= K; k++)
            {
                start = true;
                uj = true;
                ui = true;
                longs = true;
                loci = N;
                longj = 1;
                mini = N;
                indexi = N;
                any = 0;
                for (int i = 0; i < N; i++)
                {
                    indexj = N;
                    brj = 1;
                    for (int j = 0; j < N; j++)
                    { 

                        if (M[i, j] == k)
                        {
                            any++;

                            if (loci > j && mini > i)
                            {
                                mini = i;
                                loci = j;
                                indexi = i;
                                indexj = j;
                            }
                            
                            if (indexj > j)
                            {
                                indexj = j;
                                if (loci != indexj) start = false;
                            }
                            if (indexj + 1 == j && loci != j)
                            {
                                indexj = j;
                                if (mini == i) longj++;
                                brj++;
                            }
                            else uj = false;
                            if (indexi + 1 == i && mini != i) indexi = i;
                            else ui = false;
                            if (indexj == j || indexi == i) uj = true;
                        }
                        else
                        {
                            if (any == 0)
                            {
                                uj = false;
                            }
                            else uj = true;
                        }
                            
                    }
                    
                }
                if (longs == true && uj == true && start == true) result++;
            }
            Console.WriteLine(result);
        }
    }
}