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++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#define flush fflush(stdout)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pl;
const int mod = (int)1e9 + 7, INF = (int)1e9;
const int di[4] = { 1,0,-1,0 }, dj[4] = { 0,1,0,-1 };


#define N 160005
class uf {
private:
    int par[N] = {}, r[N] = {}, size[N] = {};

public:
    uf(int n) {
        int i;
        for (i = 0; i < n; i++) {
            par[i] = i;
            size[i] = 1;
        }
    }

    int root(int a) {
        if (par[a] == a) return a;
        return par[a] = root(par[a]);
    }

    void unite(int a, int b) {
        a = root(a);
        b = root(b);
        if (a == b) return;
        if (r[a] > r[b]) {
            par[b] = a;
            r[a]++;
            size[a] += size[b];
        }
        else {
            par[a] = b;
            r[b]++;
            size[b] += size[a];
        }
    }

    int show_size(int a) {
        return size[root(a)];
    }

    int group(int n) {
        int i, t, res = 0;
        bool u[N] = { false };
        for (i = 0; i < n; i++) {
            t = root(i);
            if (!u[t]) {
                res++;
                u[t] = true;
            }
        }
        return res;
    }

    bool isroot(int a) {
        return par[a] == a;
    }

};

int main(void) {
    int h, w, i, j, bl[N] = {}, wh[N] = {}, dir, ni, nj, id, nid, rid, rnid, ans;
    string s[403];

    scanf("%d%d", &h, &w);
    for (i = 0; i < h; i++)
        cin >> s[i];

    for (i = 0; i < h; i++) for (j = 0; j < w; j++)
    {
        if (s[i][j] == '#')
            bl[i * w + j] = 1;
        else
            wh[i * w + j] = 1;
    }

    uf unfi(h * w + 3);
    for (i = 0; i < h; i++) for (j = 0; j < w; j++)
    {
        for (dir = 0; dir < 4; dir++)
        {
            ni = i + di[dir];
            nj = j + dj[dir];
            if (ni >= 0 && ni < h && nj >= 0 && nj < w && s[i][j] != s[ni][nj])
            {
                id = i * w + j;
                nid = ni * w + nj;
                if (unfi.root(id) != unfi.root(nid))
                {
                    rid = unfi.root(id);
                    rnid = unfi.root(nid);
                    unfi.unite(id, nid);
                    bl[rid] = bl[rnid] = bl[rid] + bl[rnid];
                    wh[rid] = wh[rnid] = wh[rid] + wh[rnid];
                }
            }
        }
    }

    ans = 0;
    for (id = 0; id < h * w; id++)
    {
        if (unfi.isroot(id))
            ans += bl[id] * wh[id];
    }

    printf("%d\n", ans);

    return 0;
}