#include #include #include #include #include #include #include #include #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define REP(i,n) FOR(i,0,n) #define TRACE(x) cerr << #x << " = " << x << endl #define _ << " _ " << #define pb push_back #define X first #define Y second using namespace std; typedef long long ll; typedef pair pii; int n, m; vector mat; int num[1005][1005]; ll calc(){ ll sol = 0; REP(i,n) num[i][m-1] = 0; REP(j,m) num[n-1][j] = 0; for (int i = n-2; i >= 0; --i) for (int j = m-2; j >= 0; --j){ if (mat[i][j] == mat[i][j+1] && mat[i][j] == mat[i+1][j]) num[i][j] = 1 + min(num[i][j+1], num[i+1][j]); else num[i][j] = 0; sol += num[i][j]; } return sol; } void rotate(){ vector nmat; REP(j,m) nmat.pb(string(n, ' ')); REP(i,n) REP(j,m) nmat[m-1-j][i] = mat[i][j]; mat = nmat; swap(n, m); } int main(){ ios_base::sync_with_stdio(false); cin >> n >> m; REP(i,n){string s; cin >> s; mat.pb(s);} ll sol = 0; REP(i,4){ sol += calc(); rotate(); } cout << sol << endl; return 0; }