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)

#pragma G++ optimize (2)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <queue>
#define INF 0x3f3f3f3f
#define NO 1005
#define MO 100005
typedef long long ll;
//by Oliver
using namespace std;
inline ll read()
{
	char ch = ' ', last;
	ll ans = 0;
	while (ch < '0' || ch > '9')
		last = ch, ch = getchar();
	while (ch >= '0' && ch <= '9')
		ans = ans * 10 + int(ch - '0'), ch = getchar();
	if (last == '-')
		return -ans;
	return ans;
}
void write(ll x)
{
	if (x >= 10)
		write(x / 10);
	putchar(x % 10 + '0');
}
//head

ll n, m, g[NO][NO], ans, cnt[2];
const int go[4][2] = { {0, 1}, {1, 0}, {0, -1}, {-1, 0} };
bool book[NO][NO];
struct node
{
	ll now, i, j;
};
//variable

void init()
{
	memset(g, -1, sizeof(g));
	n = read(), m = read();
	for(int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
		{
			char a;
			cin >> a;
			g[i][j] = a == '#';
		}
}
//functions

int main()
{
	init();
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			if (!book[i][j])
			{
				queue<node>q;
				q.push(node{g[i][j], i, j});
				cnt[0] = cnt[1] = 0;
				while (!q.empty())
				{
					node now = q.front();
					q.pop();
					if (!now.i || !now.j || now.i > n || now.j > m || book[now.i][now.j])
						continue;
					cnt[now.now]++;
					book[now.i][now.j] = true;
					for (int k = 0; k < 4; k++)
						if (g[now.i + go[k][0]][now.j + go[k][1]] != now.now)
							q.push(node{!now.now, now.i + go[k][0], now.j + go[k][1] });
				}
				//printf("now at (%d, %d), tot = %d * %d = %d\n", i, j, cnt[0], cnt[1], cnt[1] * cnt[0]);
				ans += (ll)cnt[1] * cnt[0];
			}
	cout << ans << endl;
	return 0;
}
//main