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 JavaScript (node.js v5.12) (judge verdict: Runtime Error)

"use strict";

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function main(input) {
  // 標準入力を行ごとに分解
  var lines = input.split("\n"); // HとWを受け取る

  var _lines$0$match$slice$ = lines[0].match(/^(\d+)\s+(\d+)$/).slice(1).map(function (i) {
    return parseInt(i, 10);
  }),
      _lines$0$match$slice$2 = _slicedToArray(_lines$0$match$slice$, 2),
      H = _lines$0$match$slice$2[0],
      W = _lines$0$match$slice$2[1]; // Sたちを取り出す


  var S = lines.slice(1, H + 1); // 各マスを既に訪れたかどうかを記録する2次元配列を作成

  var visited = Array.from({
    length: H
  }, function (x) {
    return new Array(W).fill(false);
  }); // 各連結成分を深さ優先探索

  var result = 0;

  for (var x = 0; x < W; x++) {
    for (var y = 0; y < H; y++) {
      var _search = search(x, y),
          _search2 = _slicedToArray(_search, 2),
          black = _search2[0],
          white = _search2[1];

      result += black * white;
    }
  } // 答えを出力


  console.log(result);

  function search(x, y) {
    if (visited[y][x]) {
      // このマスは探索済みだ
      return [0, 0];
    }

    visited[y][x] = true;
    var black = 0,
        white = 0; // 現在の位置をカウント

    var here = S[y][x];

    if (here === "#") {
      black++;
    } else {
      white++;
    } // 上下左右を探索


    var _arr2 = [[-1, 0], [1, 0], [0, -1], [0, 1]];

    for (var _i2 = 0; _i2 < _arr2.length; _i2++) {
      var _arr2$_i = _slicedToArray(_arr2[_i2], 2),
          dx = _arr2$_i[0],
          dy = _arr2$_i[1];

      var nextx = x + dx,
          nexty = y + dy;

      if (nextx < 0 || nexty < 0 || nextx >= W || nexty >= H) {
        continue;
      } // 隣が自分と異なる場合のみ探索可能


      if (S[nexty][nextx] !== here) {
        var _search3 = search(nextx, nexty),
            _search4 = _slicedToArray(_search3, 2),
            b = _search4[0],
            w = _search4[1];

        black += b;
        white += w;
      }
    }

    return [black, white];
  }
} // 標準入力を文字列で受け取ってmain関数を実行


main(require("fs").readFileSync("/dev/stdin", "utf8"));