#include<iostream>

using namespace std;

int main(){
    int N, K;
    
    cin >> N >> K;
    
    int arr[K] = {};
    
    int M[N][N];
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++)
            cin >> M[i][j];
    }
    
    int MB[N][N] = {};
    
    for(int row = 0; row < N; row++){
        for(int col = 0; col < N; col++){
            if(MB[row][col] == 1) continue;
            
            int tv = M[row][col];
            
            int x = col;
            while(M[row][x+1] == tv && x < N) x++;
            int y = row;
            while(M[y+1][col] == tv && y < N) y++;
            
            
            for(int i = row; i <= y; i++){
                for(int j = col; j <= x; j++){
                    if(M[i][j] != tv){
                        arr[tv-1] = -1;
                    } else {
                        MB[i][j] = 1;
                    }
                }
            }
            
            if(arr[tv-1] == 1) arr[tv-1] = -1;
            if(arr[tv-1] == 0) arr[tv-1] = 1;
            
        } 
    }
    
    int sum = 0;
    for(int i = 0; i < K; i++)
        if(arr[i] == 1) sum++;
    
    cout << sum;
    return 0;    
}