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<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<bitset>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
#define FOR(i,a,n) for(int (i)=(a);(i)<(n);(i)++)
#define eFOR(i,a,n) for(int (i)=(a);(i)<=(n);(i)++)
#define SORT(i) sort((i).begin(),(i).end())
#define rSORT(i,a) sort((i).begin(),(i).end(),(a))
constexpr auto INF = 1000000000;
constexpr auto LLINF = 1LL << 62;
constexpr auto mod = 1000000007;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; }return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; }return 0; }

int h, w;
bool ever[400][400];
string field[400];
int cnt[160000][2];
int dx[4] = { -1,0,1,0 }, dy[4] = { 0,-1,0,1 };

void dfs(int i, int x, int y) {
    ever[x][y] = 1;
    cnt[i][field[y][x] == '.']++;
    FOR(j, 0, 4) {
        int _x = x + dx[j], _y = y + dy[j];
        if (_x < 0 || _x >= w || _y < 0 || _y >= h)continue;
        if (ever[_x][_y])continue;
        if (field[y][x] == field[_y][_x])continue;
        dfs(i, _x, _y);
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> h >> w;
    FOR(i, 0, h)cin >> field[i];

    int i = 0;
    FOR(y, 0, h)FOR(x, 0, w) {
        if (ever[x][y])continue;
        dfs(i++, x, y);
    }

    ll ans = 0;
    for (int i = 0; i < 160000; i++) {
        if (cnt[i][0] == 0 && cnt[i][1] == 0)break;
        ans += cnt[i][0] * cnt[i][1];
    }

    cout << ans << "\n";
}