#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e3;
int a[MAXN][MAXN], sum[10];
int main()
{
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, m, ans = 0;
cin >> n >> m;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
{
cin >> a[i][j];
sum[a[i][j]] ++;
}
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
{
int tot = 0;
if(a[i + 1][j] == a[i][j])
tot ++;
if(a[i - 1][j] == a[i][j])
tot ++;
if(a[i][j + 1] == a[i][j])
tot ++;
if(a[i][j - 1] == a[i][j])
tot ++;
ans = ans + (sum[a[i][j]] - tot - 1);
}
cout << ans;
return 0;
}
思路是求出所有数字出现的次数,再根据每个数字实时判断,如果有公共边就减去