#include using namespace std; #define PII pair #define VI vector #define VPII vector #define LL long long #define f first #define s second #define MP make_pair #define PB push_back #define LD long double #define endl '\n' #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, b, e) for (int i = (b); i <= (int)(e); i++) #define FORD(i, b, e) for (int i = (b); i >= (int)(e); i--) #define ll LL #define mp MP #define pb PB #define st f #define nd s #define eb emplace_back const int inf = 1e9 + 7; const LL INF = 1e18L + 7; #define sim template ostream & operator << (ostream &p, pair x) {return p << "<" << x.f << ", " << x.s << ">";} sim> auto operator << (ostream &p, n y) -> typename enable_if::value, decltype(y.begin(), p)>::type {int o = 0; for (auto c : y) {if (o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} sim, class s> void mini(n &p, s y) {if (p > y) p = y;} sim, class s> void maxi(n &p, s y) {if (p < y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define I(x) #x " =", (x), " " #define A(a, i) #a "[" #i " =", i, "] =", a[i], " " const int MXN = 1003; int in[MXN][MXN]; int t[MXN][MXN]; int dp[MXN][MXN]; LL res = 0; void calc(int n, int m) // obliczam z t { if(0) FOR(i, 1, n) { FOR(j, 1, m) { cerr << (char)t[i][j]; } cerr << endl; } FORD(i, n, 1) FORD(j, m, 1) { if(t[i][j] != t[i][j+1] || t[i][j] != t[i+1][j] || i == n || j == m) { dp[i][j] = 0; continue; } dp[i][j] = min(dp[i+1][j], dp[i][j+1])+1; res += max(0, dp[i][j]); // debug(i, j, dp[i][j]); } if(0) FOR(i, 1, n) { FOR(j, 1, m) { cerr << dp[i][j]; } cerr << endl; } } int main() { int n, m; scanf("%d%d", &n, &m); FOR(i, 1, n) { FOR(j, 1, m) { char ch; scanf(" %c", &ch); in[i][j] = ch; } } REP(uu, 4) { FOR(i, 1, n) { FOR(j, 1, m) { t[j][n-i+1] = in[i][j]; } } swap(n, m); FOR(i, 1, n) { FOR(j, 1, m) { in[i][j] = t[i][j]; } } calc(n, m); } cout << res << endl; }