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

namespace DobriPravougaonici
{
    class Program
    {
        static void Main(string[] args)
        {
            S:

            string[] linija1 = Console.ReadLine().Split();
            int i, j, N, K, x;
            int a, b, brojac, p, q = 0, br = 0, xd, fml = 0, dab;

            N = Convert.ToInt32(linija1[0]);
            K = Convert.ToInt32(linija1[1]);

            if (N > 2000 || N < 1 || K < 1 || K > 1000000)
            {
                Console.WriteLine("Prekrsili ste ogranicenje!");
                goto S;
            }
        
            int[,] M = new int[N, N]; 

            S1:

            for(i = 0; i < N; i++)
            {
                string[] niz = Console.ReadLine().Split();
                for(j = 0; j < N; j++)
                {
                    M[i, j] = Convert.ToInt32(niz[j]);
                    if(M[i,j] > K || M[i,j] < 1)
                    {
                        Console.WriteLine("Prekrsili ste ogranicenje!");
                        goto S1;
                    }
                }
            }

            for(x = 1; x <= K; x++)
            {
            S2:
                br = br + q;
                q = 0;
                a = 0;
                b = 0;
                fml = 0;
                dab = 0;
                brojac = 0;

                for(i = 0; i < N; i++)
                {
                    for(j = 0; j < N; j++)
                    {
                        if (M[i, j] == x)
                        {
                            if(brojac == 0)
                            {
                                a = 1;
                                b = 1;
                                brojac++;
                                q = 1;
                            }
                            else
                            {
                                if (j > 0)
                                {
                                    if (M[i, j] == M[i, j - 1] && j > 0)
                                    {
                                        b++;
                                        fml = 1;
                                    }
                                }

                                if (i > 0)
                                {
                                    if (M[i, j] == M[i - 1, j] && i > 0)
                                    {
                                        a++;
                                        xd = 0;
                                        for (p = j; p < j + b; p++)
                                        {
                                            if (M[i, p] == x) xd++;
                                        }
                                        if (xd == b)
                                        {
                                            j = j + xd - 1;
                                            q = 1;
                                            dab = 1;
                                        } 
                                    }
                                    else
                                    {
                                        if (fml == 1 && dab == 0)
                                        {
                                            q = 1;
                                        }
                                        else
                                        {
                                            q = 0;
                                        }
                                        x++;
                                        goto S2;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            Console.WriteLine(br);

        }
    }
}