#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n;
    int_fast64_t k;

    cin >> n >> k;
    int_fast64_t m[n][n];
    int_fast64_t pravilniPravougaonici = 0;

    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            cin >> m[i][j];
        }
    }
    for (int_fast64_t x = 1; x <= k; ++x)
    {
       int elementsCount = 0;
       int leftTopAngle_X = -1;
       int leftTopAngle_Y = -1;
       int rightTopAngle_X = -1;
       int rightTopAngle_Y = -1;
       int leftBottomAngle_X = -1;
       int leftBottomAngle_Y = -1;
       int rightBottomAngle_X = -1;
       int rightBottomAngle_Y = -1;
       for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (m[i][j] == x)
                {
                    ++elementsCount;
                    if (i < leftTopAngle_X || j < leftTopAngle_Y || leftTopAngle_X == -1)
                    {
                        leftTopAngle_X = i;
                        leftTopAngle_Y = j;
                    }
                    if (i > rightTopAngle_X || j < rightTopAngle_Y || rightTopAngle_X == -1)
                    {
                        rightTopAngle_X = i;
                        rightTopAngle_Y = j;
                    }
                    if (i < leftBottomAngle_X || j > leftBottomAngle_Y || leftBottomAngle_X == -1)
                    {
                        leftBottomAngle_X = i;
                        leftBottomAngle_Y = j;
                    }
                    if (i > rightBottomAngle_X || j > rightBottomAngle_Y || rightBottomAngle_X == -1)
                    {
                        rightBottomAngle_X = i;
                        rightBottomAngle_Y = j;
                    }
                }
            }
        }
        if (leftBottomAngle_X != -1)
        {
            bool pass = false;
            if (leftBottomAngle_X == leftTopAngle_X && leftBottomAngle_Y == rightBottomAngle_Y && leftTopAngle_Y == rightTopAngle_Y && rightTopAngle_X == rightBottomAngle_X)
            {
                pass = true;
            }
            if (pass == true)
            {
               int elementsInArea = 0;
               for (int i = leftTopAngle_X; i <= rightTopAngle_X; ++i)
                {
                    for (int j = leftTopAngle_Y; j <= leftBottomAngle_Y; ++j)
                    {
                        ++elementsInArea;
                    }
                }
                if (elementsCount == elementsInArea)
                {
                    ++pravilniPravougaonici;
                }
            }
        }
    }

    cout << pravilniPravougaonici << endl;




    return 0;
}