1.枚举正反对角线
2.同一条对角线上的格子按值分组,每组对答案的贡献为size*(size-1),即 每次加入一个值,对答案的贡献为组内的个数乘以2
3.对角线需要添加偏移量,放置越界以及重复
#include <bits/stdc++.h>
#define PI acos(-1)
#define all(a) (a).begin(), (a).end()
#define erjinzhi(n) __builtin_popcountll(n);
#define IOS \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
// #pragma GCC optimize(2,"Ofast","inline")
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
const char nl = '\n';
const int N = 5e3 + 10;
int mp[N][N], ans, n, m;
int main()
{
IOS;
cin >> n >> m;
for (int i = 1; i <= n; ++i)
{
for (int j = 1, x; j <= m; ++j)
{
cin >> x;
ans += (mp[j - i + n][x] + mp[j + i + m + n][x]) * 2;
mp[j - i + n][x]++;
mp[j + i + m + n][x]++;
}
}
cout << ans << nl;
return 0;
}