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)

// TEMPLATE
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = (a); (i) < (b); (i)++)
#define FORS(i, a, b) for (int i = (a); (i) <= (b); (i)++)
#define REP(i, n) for (int i = 0; (i) < (n); (i)++)
#define REPS(i, n) for (int i = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (int i = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (int i = n; (i) > 0; (i)--)
#define len(x) ((int)(x).size())
#define print(x) (cout << (x) << "\n")

// LIBRARY

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (data[y] < data[x])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

// SOLVE
void solve() {
  int H, W;
  cin >> H >> W;
  vvi C(H, vector<int>(W));
  UnionFind uf(H * W);
  REP(i, H) {
    string str;
    cin >> str;
    REP(j, W) {
      if (str[j] == '.') {
        C[i][j] = 0;
      } else {
        C[i][j] = 1;
      }
    }
  }
  REP(i, H) {
    REP(j, W) {
      if (i != 0 && C[i][j] != C[i - 1][j]) {
        uf.unite((i - 1) * W + j, i * W + j);
      }
      if (j != 0 && C[i][j] != C[i][j - 1]) {
        uf.unite(i * W + j, i * W + j - 1);
      }
    }
  }
  set<int> s;
  map<int, int> wc;
  map<int, int> bc;
  REP(i, H) {
    REP(j, W) {
      int root = uf.root(i * W + j);
      s.insert(root);
      if (C[i][j] == 0) {
        wc[root]++;
      } else {
        bc[root]++;
      }
    }
  }
  int ans = 0;
  for (set<int>::iterator it = s.begin(), end = s.end(); it != end; ++it) {
    ans += wc[*it] * bc[*it];
  }
  print(ans);
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}