#include <bits/stdc++.h>   using namespace std;   const int max_n = 2005;   string str[max_n];   int dp[max_n][max_n][4];   inline void solve() {     register int n, m, i, j, k;     long long ans = 0;     cin >> n >> m;     for (i = 0; i < n; ++i) cin >> str[i];     for (i = 0; i < n; ++i) {       dp[i][0][0] = 1;       for (j = 1; j < m; ++j) {         if (str[i][j] == str[i][j - 1])           dp[i][j][0] = dp[i][j - 1][0] + 1;         else           dp[i][j][0] = 1;       }     }     for (i = 0; i < n; ++i) {       dp[i][m - 1][1] = 1;       for (j = m - 2; j >= 0; --j) {         if (str[i][j] == str[i][j + 1])           dp[i][j][1] = dp[i][j + 1][1] + 1;         else           dp[i][j][1] = 1;       }     }     for (j = 0; j < m; ++j) dp[0][j][2] = 1;     for (i = 1; i < n; ++i) {       for (j = 0; j < m; ++j) {         if (str[i][j] == str[i - 1][j])           dp[i][j][2] = min(dp[i][j][0], min(dp[i][j][1], dp[i - 1][j][2] + 1));         else           dp[i][j][2] = 1;       }     }     for (j = 0; j < m; ++j) dp[n - 1][j][3] = 1;     for (i = n - 2; i >= 0; --i) {       for (j = 0; j < m; ++j) {         if (str[i][j] == str[i + 1][j])           dp[i][j][3] = min(dp[i][j][2], dp[i + 1][j][3] + 1);         else           dp[i][j][3] = 1;         ans += dp[i][j][3];       }     }     ans += m;     cout << ans << endl;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     solve();     return 0;   }   