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: Accepted)

#include <bits/stdc++.h>
using namespace std;

using pii = pair<int,int>;
using ll = long long;
#define rep(i, j) for(int i=0; i < (int)(j); i++)
#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)
#define all(v) v.begin(),v.end()
#define debug(x) cerr << #x << " : " << x << endl

template<class T> bool set_min(T &a, const T &b) { return a > b  ? a = b, true : false; }
template<class T> bool set_max(T &a, const T &b) { return a < b  ? a = b, true : false; }
// vector
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
template<class T> ostream& operator << (ostream &os , const vector<T> &v) { for(const T &t : v) os << "\t" << t; return os << endl; }
// pair
template<class T, class U> ostream& operator << (ostream &os , const pair<T, U> &v) { return os << "<" << v.first << ", " << v.second << ">"; }

const int INF = 1 << 30;
const ll INFL = 1LL << 60;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

class Solver {
  public:
    bool solve() {
        int H, W; cin >> H >> W;
        vector<string> G(H); cin >> G;
        ll ans = 0;
        vector<vector<int>> visited(H, vector<int>(W));
        rep(h, H) rep(w, W) {
            if(not visited[h][w]) {
                visited[h][w] = true;
                // cerr << endl;
                int dots = 0;
                int sharp = 0;
                queue<pii> que;
                que.emplace(h, w);
                while(que.size()) {
                    pii p = que.front(); que.pop();
                    int y = p.first;
                    int x = p.second;
                    // cerr << y << "," << x << endl;
                    (G[y][x] == '.' ? dots : sharp)++;
                    rep(i, 4) {
                        int ny = y + dy[i];
                        int nx = x + dx[i];
                        if(0 <= ny and ny < H and 0 <= nx and nx < W) {
                            if(G[y][x] != G[ny][nx]) {
                                if(not visited[ny][nx]) {
                                    visited[ny][nx] = true;
                                    que.emplace(ny, nx);
                                }
                            }
                        }
                    }
                }
                // cerr << " > " << dots << "," << sharp << endl;
                ans += (ll)dots * sharp;
            }
        }
        cout << ans << endl;
        return 0;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    Solver s;
    s.solve();
    return 0;
}