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: Runtime Error)

#include<bits/stdc++.h>
/*
g++ -std=c++11 -Wall -g -fsanitize=undefined -D_GLIBCXX_DEBUG aising/draft.cpp
*/
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define SZ(x) ((int)(x).size())
#define INF (1e16)
typedef long long ll;
int h, w;
char m[405][405];
char m2[10000];

int parent[100000];
int nb[100000];
int nw[100000];
int visited[100000];

void init(int n){// 初期化．全てのnodeで親を自分自身に，tree_rankを0にする．nもらわなくてもv.size()でいける
    rep(i, n){
        parent[i] = i;
        if(m2[i] == '#'){
            nb[i] = 1;
            nw[i] = 0;
        }else{
            nb[i] = 0;
            nw[i] = 1;
        }
    }
}

int root(int x){// node xのrootを返す．parent[x] = root(parent[x])で親をrootにつなぎ替える
    return x == parent[x] ? x : parent[x] = root(parent[x]);
}

bool same(int x, int y){// 同じ木に属するかどうか判定する
    return root(x) == root(y);
}

void unite(int x, int y){// xの属する木とyの属する木を連結する．rankができるだけ低くなるように処理を行う
    x = root(x);
    y = root(y);

    if(x != y){
        parent[y] = x;
        nb[x] += nb[y];
        nw[x] += nw[y];
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);//おまじないでcin, cout早くする

    cin >> h >> w;
    int cnt = 0;
    rep(i, h){
        rep(j, w){
            cin >> m[i][j];
            m2[cnt] = m[i][j];
            cnt++;
        }
    }

    init(h * w);

    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(i != h - 1){
                if(m[i][j] != m[i+1][j]){
                    unite(i * w + j, (i+1) * w + j);
                }
            }
            if(j != w - 1){
                if(m[i][j] != m[i][j+1]){
                    unite(i * w + j, i * w + j + 1);
                }
            }
        }
    }

    rep(i, h * w){
        visited[i] = 0;
    }

    ll ans = 0;
    rep(i, h * w){
        int k = root(i);
        if(visited[k] == 0){
            ans += nb[k] * nw[k];
            visited[k] = 1;
        }
    }

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