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 <bits/stdc++.h>
#include <cassert>

using namespace std;
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os; }
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {os << "(" << p.first << ", " << p.second << ")"; return os;}

struct UnionFind {
    vector<int> par;
    UnionFind(int n) : par(n, -1) {};
    int root(int v) {
        if (par[v] < 0) return v;
        return par[v] = root(par[v]);
    }
    int size(int v) {
        return -par[root(v)];
    }
    bool unionSet(int a, int b) {
        a = root(a);
        b = root(b);
        if (a == b) return false;
        if (size(a) > size(b)) swap(a, b);
        par[b] += par[a];
        par[a] = b;
        return true;
    }
    bool isSame(int a, int b) {
        return root(a) == root(b);
    }
};


void solve() {
    int H, W;
    cin >> H >> W;
    vector<string> S(H);
    for (int i = 0; i < H; i++) {
        cin >> S[i];
    }
    int dx[] = {0, -1, 0, 1};
    int dy[] = {-1, 0, 1, 0};

    auto get = [&](int i, int j) {
        return W * i + j;
    };

    UnionFind uf(H * W);
    for(int i = 0; i < H; i++) {
        for(int j = 0; j < W; j++) {
            for(int k = 0; k < 4; k++) {
                int tx = j + dx[k];
                int ty = i + dy[k];
                if (tx < 0 || tx >= W || ty < 0 || ty >= H) continue;

                assert(get(ty, tx) < H * W);
                if (S[i][j] != S[ty][tx]) {
                    uf.unionSet(get(i, j), get(ty, tx));
                }
            }
        }
    }

    map<int, ll> black;
    map<int, ll> white;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (S[i][j] == '.') white[uf.root(get(i, j))]++;
            else black[uf.root(get(i, j))]++;
        }
    }

    ll ans = 0;
    for(auto e : white) {
        ans += e.second * black[e.first] / 2;
    }
    for(auto e : black) {
        ans += e.second * white[e.first] / 2;
    }
    cout << ans << endl;
    
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(16);
    solve();
    return 0;
}