#include <stdio.h>
#include <stdlib.h>

typedef struct{
     int x,y,l,h;
}Box;

int n,k,c = 0;
int a[2000][2000];
Box box[100];

int main()
{
    for(int i = 0;i < 100;i++){
        box[i].x = -1;
        box[i].l = 1;
        box[i].h = 1;
    }

    scanf("%d%d",&n,&k);

    for(int i = 0;i < n;i++){
        for(int j = 0;j < n;j++){
            scanf("%d",&a[i][j]);
        }
    }

    for(int i = 0;i < n;i++){
        for(int j = 0;j < n;j++){
            if(box[a[i][j]].x == -2)
                continue;
            if(box[a[i][j]].x == -1)
            {
                box[a[i][j]].x = j;
                box[a[i][j]].y = i;
            }else{
                if(box[a[i][j]].x + box[a[i][j]].l == j)
                    box[a[i][j]].l++;
                else if(box[a[i][j]].y + box[a[i][j]].h == i)
                    box[a[i][j]].h++;
                else
                    box[a[i][j]].x = -2;
            }
        }
    }

    for(int i = 0;i < 100;i++)
        if(box[i].x > -1)
            c++;

    printf("%d",c);
    return 0;
}