#include #include using namespace std; int tab[1001][1001]; char mapa2[1001][1001]; char mapa[1001][1001]; void obroc( int n ) { for ( int i = 1; i <= n; ++i ) { for ( int j = 1; j <= n; ++j ) { mapa2[j][n-i+1] = mapa[i][j]; } } for ( int i = 1; i <= n; ++i ) { for ( int j = 1; j <= n; ++j ) { mapa[i][j] = mapa2[i][j]; } } } int main() { int n, m; scanf( "%d%d", &n, &m ); for ( int i = 1; i <= n; ++i ) { for ( int j = 1; j <= m; ++j ) { cin >> mapa[i][j]; } } int combo; char last; int answer = 0; int xd; for ( int q = 0; q < 4; ++q ) { xd = 0; /*for ( int i = 1; i <= n; ++i ) { for ( int j = 1; j <= m; ++j ) { cout << mapa[i][j]; } cout << endl; }*/ for ( int i = 1; i <= n; ++i ) { combo = 0; last = 0; for ( int j = 1; j <= m; ++j ) { if ( mapa[i][j] == last ) { combo++; } else { combo = 1; last = mapa[i][j]; } tab[i][j] = 1; if ( mapa[i-1][j-1] == mapa[i][j] ) { tab[i][j] = min( combo, tab[i-1][j-1]+1 ); answer += tab[i][j]-1; xd += tab[i][j]-1; } //cout << tab[i][j]; } //cout << endl; } //cout << "\t" << xd << endl; obroc( max( n, m ) ); swap( n, m ); } printf( "%d\n", answer ); }