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: Wrong Answer)


#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <queue>

typedef unsigned long long ULLONG;
typedef long long LLONG;
static const LLONG MOD_NUM = 1000000007;

template<class _T> static void getint(_T& a) {
	const char* fmt = " %d";
	if (sizeof(_T) == sizeof(long long)) {
		fmt = " %lld";
	}

	if (scanf(fmt, &a) < 0) {
		printf("g1int Error\n");
	}
}
template<class _T> static void getint(_T& a, _T& b) {
	const char* fmt = " %d %d";
	if (sizeof(_T) == sizeof(long long)) {
		fmt = " %lld %lld";
	}

	if (scanf(fmt, &a, &b) < 0) {
		printf("g2int Error\n");
	}
}
template<class _T> static void getint(_T& a, _T& b, _T& c) {
	const char* fmt = " %d %d %d";
	if (sizeof(_T) == sizeof(long long)) {
		fmt = " %lld %lld %lld";
	}

	if (scanf(fmt, &a, &b, &c) < 0) {
		printf("g3int Error\n");
	}
}

static void AIsing2019C();

#if 1
int main()
{
	AIsing2019C();
	fflush(stdout);
	return 0;
}
#endif

static void AIsing2019C()
{
	int H, W;
	getint(H, W);

	std::vector<std::string> grid(H);
	for (int i = 0; i < H; i++) {
		std::cin >> grid[i];
	}
	std::vector<std::vector<int>> flag(H, std::vector<int>(W, 0));
	std::vector<std::pair<int, int>> pairs;

	int dh[] = { 1, -1, 0, 0 };
	int dw[] = { 0, 0, -1, 1 };
	for (int h = 0; h < H; h++) {
		for (int w = 0; w < W; w++) {
			if ((grid[h][w] == '#') && (flag[h][w] == 0)) {
				std::pair<int, int> bwPair(0, 0);

				std::queue<std::pair<int, int>> que;
				que.push(std::pair<int, int>(h, w));

				flag[h][w] = 1;
				bwPair.first++;

				while (!que.empty()) {
					bool nowBlack = false;
					std::pair<int, int> pos = que.front(); que.pop();

					if (grid[pos.first][pos.second] == '#') {
						nowBlack = true;
					}

					for (int i = 0; i < 4; i++) {
						int nh = pos.first + dh[i];
						int nw = pos.second + dw[i];

						if ((nh >= 0) && (nw >= 0) && (nh < H) && (nw < W) && (flag[nh][nw] == 0)) {
							if ((nowBlack && (grid[nh][nw] != '#')) || (!nowBlack && (grid[nh][nw] == '#'))) {
								if (nowBlack) {
									bwPair.second++;
								}
								else {
									bwPair.first++;
								}
								flag[nh][nw] = 1;
								que.push(std::pair<int, int>(nh, nw));
							}
						}
					}
				}
				if (bwPair.second != 0) {
					pairs.push_back(bwPair);
				}
			}
		}
	}
	LLONG ans = 0;
	for (auto it = pairs.begin(); it != pairs.end(); it++) {
		ans += (LLONG)(it->first * it->second);
	}
	printf("%lld\n", ans);
}