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 <iostream>
#include <cmath>
#include <vector>
#include <string>
using std::endl;
using std::cin;
using std::cout;
using std::vector;
using std::pair;
using std::string;




int main(void)
{
    int32_t h, w;
    cin >> h >> w;
    string s[h];
    uint32_t s_t[h][w] = {};
    for (uint32_t i = 0; i < h; i++) {
        cin >> s[i];

    }
    
    int32_t regVal = 1;

    for (uint32_t i = 0; i < h; i++) {
        for (uint32_t j = 0; j < w; j++) {
            //cout << "  " << i << "," << j << ": " << regVal << ": " << regCount << endl;;
            if (i==0 && j==0) {
                s_t[i][j] = regVal;
            } else {
                if (i > 0 && s_t[i-1][j] != 0) {
                    if (s[i-1].substr(j,1) != s[i].substr(j,1)){
                        s_t[i][j] = s_t[i-1][j];
                        continue;
                    }
                }
                if (j > 0 && s_t[i][j-1] != 0) {
                    if (s[i].substr(j-1,1) != s[i].substr(j,1)){
                        s_t[i][j] = s_t[i][j-1] ;
                        continue;
                    }
                }
                regVal++;
                s_t[i][j] = regVal;
                
            }
        }
    }
    
    uint32_t answer = 0;
    for (uint32_t r = 1; r <= regVal; r++) {
        uint32_t countBk = 0;
        uint32_t countWt = 0;
        for (uint32_t i = 0; i < h; i++) {
            for (uint32_t j = 0; j < w; j++) {
                if (r == s_t[i][j]) {
                    if (s[i].substr(j,1) == "#") {
                        countBk++;
                    } else {
                        countWt++;
                    }
                }
                //cout << s_t[i][j];
            }
            //cout << endl;
        }
        if (countBk > 0 && countWt > 0) {
            answer += countBk*countWt;
        }
        
    }

    cout << answer << endl;

    return 0;
}