#include <bits/stdc++.h>

using namespace std;

#define INFI 987654321
#define INFL 987654321987654321
#define MOD 1000000007

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template<typename T>
void read(T, int);

template<typename T>
void write(T, int);

template<typename T>
int sumFor(T, int, int);

template<typename T>
int sumFor(T, int);

int power(int, int, int);
int power(int, int);
void add(int&, int&);
int getRand();
int max(int, int, int);

/// //MAIN// ///

//int a[500005];
int mat[2005][2005];
bool done[1000005];
int cnt[1000005];

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, k;
    cin >> n >> k;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            cin >> mat[i][j];
            cnt[mat[i][j]]++;
        }
    }

    int ttl = 0;

    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            if(done[mat[i][j]]){
                continue;
            }

            int cd = mat[i][j];
            done[cd] = true;
            int a = 0;
            int b = 0;

            for(int k = j; k < n; ++k){
                if(mat[i][k] == cd){
                    ++a;
                }else{
                    break;
                }
            }

            for(int k = i; k < n; ++k){
                if(mat[k][j] == cd){
                    ++b;
                }else{
                    break;
                }
            }

            bool isrect = true;
            for(int x = i; x < i + b; x++){
                for(int y = j; y < j + a; ++y){
                    if(mat[i][j] != cd){
                        isrect = false;
                        break;
                    }
                }
            }

            if(isrect && a * b == cnt[cd]){
                ttl++;
            }
        }
    }

    cout << ttl << "\n";
    return 0;
}

/// /////////////////////////////////////////////

int max(int m_a, int m_b, int m_c){
    if(m_c >= m_a && m_c >= m_b){
        return m_c;
    }
    return m_a >= m_b ? m_a : m_b;
}

int getRand(){
    return rng();
}

void add(int& m_a, int& m_b){
    m_a = (m_a + m_b) % MOD;
}

template<typename T>
void write(T m_pnter, int m_size){
    for(int i = 0; i < m_size; ++i){
        cout << m_pnter[i] << " ";
    }
    cout << "\n";
}

template<typename T>
void read(T m_pnter, int m_size){
    for(int i = 0; i < m_size; ++i){
        cin >> m_pnter[i];
    }
}

template<typename T>
int sumFor(T m_pnter, int m_l, int m_r){
    int m_sum = 0;
    for(int i = m_l; i < m_r; ++i){
        m_sum += m_pnter[i];
    }
    return m_sum;
}

template<typename T>
int sumFor(T m_pnter, int m_r){
    int m_sum = 0;
    for(int i = 0; i < m_r; ++i){
        m_sum += m_pnter[i];
    }
    return m_sum;
}

int power(int m_a, int m_b, int m_mod){
    int m_res = 1;
    while(m_b > 0){
        if(m_b % 2 == 1){
            m_res = (m_res * m_a) % m_mod;
        }
        m_b /= 2;
        m_a = (m_a * m_a) % m_mod;
    }
    return m_res;
}

int power(int m_a, int m_b){
    int m_res = 1;
    while(m_b > 0){
        if(m_b % 2 == 1){
            m_res = m_res * m_a;
        }
        m_b /= 2;
        m_a = m_a * m_a;
    }
    return m_res;
}