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>
const int INF = 1e9;
const int MOD = 1e9+7;
using LL = long long;
const LL LINF = 1e18;
const double EPS = 1e-10;
using namespace std;
#define COUT(v) cout<<(v)<<endl
#define CIN(n)  int(n);cin >> (n)
#define LCIN(n) LL(n);cin >> (n)
#define SCIN(n) string(n);cin >> (n)
#define YES(n) cout<<((n)? "YES" : "NO")<<endl
#define Yes(n) cout<<((n)? "Yes" : "No")<<endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE"  ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible"  ) <<endl

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)

#define FOREACH(x,a) for(auto& (x) : (a) )

#define ALL(obj) (obj).begin(),(obj).end()

#define P pair<int,int>
#define I vector<int>
#define pb(v) push_back(v)
#define V vector
#define rt return
#define rmsame(a) sort(ALL(a)),a.erase(unique(ALL(a)), a.end())
#define ENDL cout<<endl
#define MAXE(a,b) a=max(a,b)
#define MINE(a,b) a=min(a,b)

typedef string::const_iterator State;
class PalseError {};
class Edge{
public:
    int from,to,value;
    Edge(LL a,LL b,LL c){
        from = a;
        to = b;
        value = c;
    }
    Edge(LL a,LL b){
        from = a;
        to = b;
    }
};

class UnionFind{
private:
    std::vector<int> par;
    std::vector<int> rank;
public:
    void init(int N){
        par.resize(N);
        rank.resize(N);
        for(int a = 0;a < N;a++)
        {
            par[a]=-1;
            rank[a]=0;
        }
    }
    int root(int a)
    {
        if(par[a] < 0)return a;
        else return par[a] = root(par[a]);
    }
    void connect(int a,int b)
    {
        a = root(a);
        b = root(b);
        if(a==b)return;

        if(rank[a] < rank[b]){
            par[a] = b;
        }else{
            par[b] = a;
            if(rank[a] == rank[b])rank[a]++;
        }
    }
    bool check(int a,int b){
        a = root(a);
        b = root(b);
        if(a == b)return true;
        else return false;
    }
};
int H,W;
int decode(int i,int j){
    return i*W+j;
}
/*
pair<int,int> uku(int d){
    return make_pair(d/W,d%W);
}
*/
int main(){
    cin >> H >> W;
    UnionFind Uni;
    Uni.init(H*W);
    vector<string> vec;
    for(int a = 0;a < H;a++){
        SCIN(str);
        vec.push_back(str);
    }
    int dx[] = {1,0,-1,0};
    int dy[] = {0,1,0,-1};
    for(int a = 0;a < H;a++){
        for(int b = 0;b < W;b++){
            for(int index = 0;index < 4;index++){
                if(0 <= dx[index] + a && dx[index] + a <= H-1 && 0 <= b + dy[index] && b+ dy[index] <= W-1){
                    if(vec.at(a).at(b)!=vec.at(a+dx[index]).at(b+dy[index]))Uni.connect(decode(a,b),decode(dx[index]+a,dy[index]+b));
                }
            }
        }
    }
    //rt 0;
    map<int,pair<int,int>> mp;
    for(int a = 0;a < H ;a++){
        for(int b = 0;b < W;b++){
          //  cout<<Uni.root(decode(a,b))<<" ";
            if(vec.at(a).at(b) == '#')mp[Uni.root(decode(a,b))].second++;
            else mp[Uni.root(decode(a,b))].first++;
        }
      //  cout<<endl;
    }
    LL ans = 0;
    for(auto a : mp){
        ans += a.second.second*a.second.first;
    }
    COUT(ans);
    return 0;
}