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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System.Collections.Generic;
using System;

public class P
{
    public int b { get; set; }
    public int w { get; set; }
}

public class UnionFind
{
    private int[] data;
    public UnionFind(int size)
    {
        data = new int[size];
        for (int i = 0; i < size; i++) data[i] = -1;
    }
    public bool Unite(int x, int y)
    {
        x = Root(x);
        y = Root(y);
        if (x != y)
        {
            if (data[y] < data[x])
            {
                var tmp = y;
                y = x;
                x = tmp;
            }
            data[x] += data[y];
            data[y] = x;
        }
        return x != y;
    }
    public bool IsSameGroup(int x, int y) => Root(x) == Root(y);
    public int Root(int x) => data[x] < 0 ? x : data[x] = Root(data[x]);
    public int getMem(int x) => -data[Root(x)];
}


public class Hello
{
    public static void Main()
    {
        string[] line = Console.ReadLine().Trim().Split(' ');
        var h = int.Parse(line[0]);
        var w = int.Parse(line[1]);
        var map = new bool[h, w];
        var sno = new int[h, w];
        var p = 0;
        for (int i = 0; i < h; i++)
        {
            var s = Console.ReadLine().Trim();
            for (int j = 0; j < w; j++)
            {
                if (s[j] == '#') map[i, j] = true;
                sno[i, j] = p++;
            }
        }
        getAns(h, w, map, sno);

    }
    static void getAns2(int h, int w, bool[,] map, int[,] sno, UnionFind uf)
    {
        var d = new Dictionary<int, P>();
        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++)
            {
                var p = uf.Root(sno[i, j]);
                if (!d.ContainsKey(p)) d[p] = new P();
                if (map[i, j]) d[p].b++;
                else d[p].w++;
            }
        var ans = 0;
        foreach (var x in d)
            ans += x.Value.b * x.Value.w;
        Console.WriteLine(ans);
    }
    static void getAns(int h, int w, bool[,] map, int[,] sno)
    {
        var dx = new int[] { 0, 1 };
        var dy = new int[] { 1, 0 };
        var uf = new UnionFind(h * w);
        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++)
                for (int k = 0; k < 2; k++)
                {
                    var nx = i + dx[k];
                    var ny = j + dy[k];
                    if (nx < h && ny < w &&
                      ((map[i, j] && !map[nx, ny]) | (!map[i, j] && map[nx, ny])))
                        uf.Unite(sno[i, j], sno[nx, ny]);
                }
        getAns2(h, w, map, sno, uf);
    }
}