Problem:
Score : 300 points

Problem Statement

There is a grid with H rows and W columns, where each square is painted black or white.

You are given H strings S_1, S_2, ..., S_H, each of length W.
If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is #; if that square is painted white, the j-th character in the string S_i is ..

Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:

There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...

Constraints

1 \leq H, W \leq 400

|S_i| = W (1 \leq i \leq H)

For each i (1 \leq i \leq H), the string S_i consists of characters # and ..

Input

Input is given from Standard Input in the following format:

H W
S_1
S_2
:
S_H

Output

Print the answer.

Sample Input 1

3 3
.#.
..#
#..

Sample Output 1

10

Some of the pairs satisfying the condition are ((1, 2), (3, 3)) and ((3, 1), (3, 2)), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.

Sample Input 2

2 4
....
....

Sample Output 2

0

Sample Input 3

4 3
###
###
...
###

Sample Output 3

6

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std ;
inline long long Readin() {
	long long K = 0 , F = 1 ; char C = ' ' ;
	while( C < '0' or C > '9' ) F = C == '-' ? -1 : 1 , C = getchar() ;
	while( C <= '9' and C >= '0' ) K = ( K << 1 ) + ( K << 3 ) + C - '0' , C = getchar() ;
	return F * K ;
}
int N , M ;
char S[1005][1005] ;
int Map[1005][1005] ;
bool Vis[1005][1005] ;
const int Mx[6] = { 0 , 1 , 0 , 0 , -1 , 0 } ;
const int My[6] = { 0 , 0 , 1 , -1 , 0 , 0 } ;
long long Ans[5] , Tot ;
void Dfs( int X , int Y ) {
	if( X < 1 or Y < 1 or X > N or Y > M or Vis[X][Y] ) return ;
	Vis[X][Y] = true ;
	++Ans[Map[X][Y]] ;
	for(int i = 1 ; i <= 4 ; ++i ) if( S[X][Y] != S[X+Mx[i]][Y+My[i]] ) Dfs( X+Mx[i] , Y+My[i] ) ;
}
inline char Getchar() {
	register char C = getchar() ;
	while( C != '.' and C != '#' ) C = getchar() ;
	return C ;
}
int main() {
	N = Readin() ;
	M = Readin() ;
	for(register int i = 0 ; ++i <= N ; )
		for(register int j = 0 ; ++j <= M ; )
			S[i][j] = Getchar() , Map[i][j] = ( S[i][j] == '#' ) ;
	for(register int i = 0 ; ++i <= N ; )
		for(register int j = 0 ; ++j <= M ; )
			if( not Vis[i][j] ) {
				Ans[0] = Ans[1] = 0 ;
				Dfs( i , j ) ;
				Tot += Ans[0] * Ans[1] ;
			}
	return not printf( "%lld\n" , Tot ) ;
}