#include <iostream>

using namespace std;

int main()
{
    int resenje = 0;
    int n, k;
    cin >> n >> k;
    int a[n][n];
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
            cin >> a[i][j];
    }
    int nizk[k];
    int x[n];
    int y[n];
    int brjk = 0;
    for(int i = 0; i < n; i++)
    {
        nizk[i]=-1;
        x[i]=0;
        y[i]=0;
    }
    bool p = false;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            p=false;
            for(int m = 0; m < brjk; m++)
            {
                if(a[i][j] == nizk[m])
                {
                    p=true;
                }
            }
            if(!p)
            {
                int br = a[i][j];
                nizk[brjk] = a[i][j];
                brjk++;
                for(int ii = 0; ii < n; ii++)
                {
                    for(int jj = 0; jj < n; jj++)
                    {
                        if(a[ii][jj] == br)
                        {
                            x[ii]++;
                            y[jj]++;
                        }
                    }
                }
                int qx=2,qy=2;
                int gx = 0;
                int gy = 0;
                int suma1=0;
                for(int ii = 1; ii < n; ii++)
                {
                    if(x[ii] != x[ii-1]) gx++;
                    if(y[ii] != y[ii-1]) gy++;
                }
                for(int ii = 0; ii < n; ii++)
                {
                    if(x[ii]!=0) qx = x[ii];
                    if(y[ii]!=0) qy = y[ii];
                    suma1+=x[ii];
                }
                if(gx < 3 && gy < 3 && suma1 == qx*qy)
                    resenje++;
                for(int ii = 0; ii < n; ii++)
                {
                    x[ii] = 0;
                    y[ii] = 0;
                }
            }
        }
    }
    cout << resenje;
    return 0;
}