#include <stdio.h>
#include <vector>

using namespace std;

int main()
{
    vector<int> numsWentTrough;
    int N, K;
    scanf("%d %d",&N,&K);
    int polje[N][N];
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
            scanf("%d",&polje[i][j]);
        }
    }

    for(int i = 0; i < N; i++)
    {
        int lastI = i;
        for(int j = 0; j < N; j++)
        {
            int sorta = polje[i][j];
            int toLeft = 0;
            if(sorta != 0)
            {
                bool isRect = true;
                while(polje[i][j+toLeft] == sorta)
                {
                    polje[i][j+toLeft] = 0;
                    toLeft++;
                }

                while(polje[++i][j] == sorta)
                {
                    int tempToLeft = 0;
                    while(polje[i][j+tempToLeft] == sorta)
                    {
                        polje[i][j+tempToLeft] = 0;
                        tempToLeft++;
                    }

                    isRect = tempToLeft == toLeft;

                    if(!isRect)
                    {
                        numsWentTrough.push_back(-sorta);
                        break;
                    }
                }

                if(isRect)
                {
                    bool add = true;
                    for(int i = 0; i < numsWentTrough.size(); i++)
                    {
                        if(numsWentTrough[i] == sorta)
                        {
                            add = false;
                            numsWentTrough[i]*= -1;
                        }
                        else if (numsWentTrough[i]*-1 == sorta)
                        {
                            add = false;
                        }
                    }

                    if(add)
                    {
                        numsWentTrough.push_back(sorta);
                    }
                }
            }

            if(toLeft > 0)
                j+= toLeft-1;
            i = lastI;
        }
    }

    int br = 0;
    for(int i = 0; i < numsWentTrough.size(); i++)
        if(numsWentTrough[i] > 0)
            br++;

    printf("%d",br);

    return 0;
}