#include #include #include #include #include using namespace std; using LL = long long; int t[1008][1008]; int t1[1008][1008]; int t2[1008][1008]; int t3[1008][1008]; int t4[1008][1008]; int jobbra[1008][1008]; LL szamol(int n, int m) { /*for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){ cout << (char)t[i][j] << " "; } cout << endl; }*/ for (int i = 0; i < n; i++){ jobbra[i][m-1] = 1; for (int j = m-2; j >= 0 ; j--){ jobbra[i][j] = (t[i][j] == t[i][j+1] ? 1+jobbra[i][j+1] : 1); } } //cout << "jobbra: " << endl; /*for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){ cout << jobbra[i][j] << " "; } cout << endl; }*/ LL ans = 0LL; for (int j = 0; j < m; j++){ LL curr = 1LL; for (int i = n-2; i >= 0; i--){ if (t[i][j] == t[i+1][j]) { curr = min(curr+1LL, (LL)jobbra[i][j]); } else { curr = 1LL; } /*if (jobbra[i][j] > curr && t[i][j] == t[i+1][j]) { curr++; } else { curr = 1LL; }*/ ans += curr-1; //cout << i << " " << j << " " << curr << " " << ans << " " << (char)t[i][j] << " " << (char)t[i+1][j] << endl; } } return ans; } int main() { int n,m; //scanf("%d %d", &n, &m); cin>>n>>m; for (int i = 0; i < n; i++){ string s; cin>>s; for (int j = 0; j < m; j++){ t1[i][j] = int(s[j]); t2[j][n-1-i] = int(s[j]); t3[n-1-i][m-1-j] = int(s[j]); t4[m-1-j][i] = int(s[j]); } } //cout << "elso" << endl; LL ans = 0; for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){ t[i][j] = t1[i][j]; } } /*for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){ cout << (char)t[i][j] << " "; } cout << endl; }*/ ans += szamol(n,m); //cout << "masodik" << endl; for (int i = 0; i < m; i++){ for (int j = 0; j < n; j++){ t[i][j] = t2[i][j]; } } /*for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){ cout << (char)t[i][j] << " "; } cout << endl; }*/ ans += szamol(m,n); //cout << "harmadik" << endl; for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){ t[i][j] = t3[i][j]; } } /*for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){ cout << (char)t[i][j] << " "; } cout << endl; }*/ ans += szamol(n,m); //cout << "negyedik" << endl; for (int i = 0; i < m; i++){ for (int j = 0; j < n; j++){ t[i][j] = t4[i][j]; } } /*for (int i = 0; i < m; i++){ for (int j = 0; j < n; j++){ cout << (char)t[i][j] << " "; } cout << endl; }*/ ans += szamol(m,n); cout << ans << endl; return 0; }