#include<bits/stdc++.h>
using namespace std;

const int N = 2e3 + 10, MAX = 1e6 + 10;

int a[N][N];
bool visited[N][N];
int c[MAX];
int sol[N][N];

int solve(int y, int x, int n) {
    int h = 1;
    int i = y;
    while(i < n - 1 && a[i][x] == a[i + 1][x]) {
        h++;
        i++;
    }

    int l = 0;

    while(a[y][x + l] == a[y][x + l + 1] && x + l < n)
        l++;

    l++;

    for(i = y ; i < y + h ; i++) {

        visited[i][x] = 1;
        // cout << a[i][x] << " ";
        int curr = 0;
        while(a[i][x + curr] == a[i][x + curr + 1] && curr + 1 < n) {
            visited[i][x + curr ] = 1;
            visited[i][x + curr + 1] = 1;

            curr++;
        }
        // cout << curr << " ";
        if(curr + 1 != l) 
            return 0;
    }
    return (l) * (h);
}

int main() {
    int n, k;
    scanf("%d%d", &n, &k);

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

    for(int i = 0 ; i < n ; i++) 
        for(int j = 0 ; j < n ; j++) 
            if(!visited[i][j])
                sol[i][j] = solve(i, j, n);

    int res = 0;

    for(int i = 0 ; i < n ; i++) {
        for(int j = 0 ; j < n ; j++) {
            if(c[a[i][j]] == sol[i][j]) {
                res++;
            }
        }
    }

    cout << res;

    return 0;
}