#include <iostream>
#include <vector>

using namespace std;

vector<vector<bool>> reseno;
vector<int> brPravougaonika;
int N, K;

void nadjiPravougaonike(const vector<vector<int>>& f, int i, int j)
{
    int voce = f[i][j];
    int y = i + 1;
    int x = j + 1;
    reseno[i][j] = true;
    //cout << "resi: " << i << j << endl;
    while (x < N)
    {
        if (f[i][x] != voce || reseno[i][x])
            break;

        reseno[i][x] = true;
        //cout << "resi: " << i << x << endl;
        x++;
    }
    bool kontrola = true;
    while (y < N)
    {
        if (f[y][j] != voce || reseno[y][j])
            break;

        for (int znj = j; znj < x; znj++)
            if (f[y][znj] != voce || reseno[y][znj])
                kontrola = false;
        if (kontrola)
        {
            for (int znj = j; znj < x; znj++)
            {
                reseno[y][znj] = true;
                //cout << "resi: " << y << znj << endl;
            }
        }
        else
            break;

        y++;
    }

    brPravougaonika[voce]++;
    //cout << "broj: " << brPravougaonika[voce] << endl;
}

int main()
{


    cin >> N;
    cin >> K;
    vector<vector<int>> farma(N);
    brPravougaonika = vector<int>(K, 0);//hm
    // stablo izmedju [leva, desna]
    //vector<int> levagranica(K, 0);
    //vector<int> desnagranica(K, 4);
    reseno.resize(N);

    for (int i = 0; i < N; i++)
    {
        reseno[i].resize(N);
        farma[i].resize(N);
        for (int j = 0; j < N; j++)
        {
            cin >> farma[i][j];
            reseno[i][j] = false;
        }

    }

    for (int x = 0; x < 2 * N; x++)
    {
        for (int i = x; i >= 0; i--)
        {
            int j = x - i;
            if (i < N && j < N && !reseno[i][j])
            {
                //cout << i << " " << j << endl;
                nadjiPravougaonike(farma, i, j);
            }
        }
    }

    int res = 0;
    for (int i = 0; i < K; i++)
        if (brPravougaonika[i] == 1)
            res++;

    cout << res << endl;
    return 0;
}