#include using namespace std; #define LL long long //#define int LL #define PB push_back #define VI vector #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define REP(i,n) FOR(i, 0, (int)n - 1) #define PII pair #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define st first #define nd second template void mini(C & a4, C b4){a4 = min(a4, b4);} template void maxi(C & a4, C b4){a4 = max(a4, b4);} template void _dbg(const char * sdbg, TH h){ cerr< void _dbg(const char * sdbg, TH h, TA... a){ while(*sdbg!=',') cerr<<*sdbg++; cerr<<'='< ostream & operator<<(ostream & os, vector V){ os<<"[";for(auto vv: V) os < ostream & operator<<(ostream & os , pair P){ return os << "(" << P.st <<","<= 1; j--) { if (board[i - 1][j] != board[i][j] || board[i][j + 1] != board[i][j]) { dpRightUp[i][j] = 1; continue; } dpRightUp[i][j] = min(dpRightUp[i - 1][j], dpRightUp[i][j + 1]) + 1; } } for (int i = n; i >= 1; i--) { for (int j = 1; j <= m; j++) { if (board[i + 1][j] != board[i][j] || board[i][j - 1] != board[i][j]) { dpLeftDown[i][j] = 1; continue; } dpLeftDown[i][j] = min(dpLeftDown[i + 1][j], dpLeftDown[i][j - 1]) + 1; } } for (int i = n; i >= 1; i--) { for (int j = m; j >= 1; j--) { if (board[i + 1][j] != board[i][j] || board[i][j + 1] != board[i][j]) { dpRightDown[i][j] = 1; continue; } dpRightDown[i][j] = min(dpRightDown[i + 1][j], dpRightDown[i][j + 1]) + 1; } } long long ans = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { //debug(i, j, dpLeftUp[i][j], dpLeftDown[i][j], dpRightUp[i][j], dpRightDown[i][j]); ans += dpLeftUp[i][j] + dpLeftDown[i][j] + dpRightUp[i][j] + dpRightDown[i][j] - 4; } } printf("%lld\n", ans); return 0; }