#include #include #include #include #include #include using namespace std; #define REP(A,B) for(int (A)=0;(A)<(B);(A)++) char SRC[1111][1111]; char SRC2[1111][1111]; int P[1111][1111]; int M[1111][1111]; int n,m; long long total = 0; void go() { memset(M, 0, sizeof(M)); memset(P, 0, sizeof(P)); /* REP(i, n) { REP(j, m) { printf("%c", SRC[i][j]); } printf("\n"); } printf("\n");*/ for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) P[i][j] = (j == 0 || SRC[i][j] != SRC[i][j-1]) ? 1 : P[i][j-1]+1; } for(int i = n-1; i >= 0; i--) { for(int j = 0; j < m; j++) { if(i < n-1 && SRC[i][j] == SRC[i+1][j]) { M[i][j] = min(M[i+1][j]+1, P[i][j]); } else { M[i][j] = 1; } total += M[i][j]-1; } } } void transpose() { REP(i, n) REP(j, m) SRC2[j][i] = SRC[i][j]; REP(i, m) REP(j, n) SRC[i][j] = SRC2[m-i-1][j]; swap(n, m); } int main() { scanf("%d%d", &n, &m); REP(i, n) scanf("%s", SRC[i]); go(); transpose(); go(); transpose(); go(); transpose(); go(); printf("%lld\n", total); return 0; }