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;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using static MyIO;


public class C
{
	public static readonly int[] dh = new int[4] { -1, 0, 1, 0 };
	public static readonly int[] dw = new int[4] { 0, -1, 0, 1 };

	public static void Main()
	{
		int H = GetInt();
		int W = GetInt();
		char[,] grid = new char[H, W];		
		int[] numWhite = new int[H * W];
		int[] numBlack = new int[H * W];
		for(int h = 0; h < H; h++)
		{
			string S = GetString();
			for(int w = 0; w < W; w++)
			{
				grid[h, w] = S[w];

				int v = h * W + w;
				if(grid[h, w] == '.')
					numWhite[v] = 1;
				else
					numBlack[v] = 1;
			}
		}

		var uf = new UnionFindEx(H * W, numWhite, numBlack);
		for(int h = 0; h < H; h++)
		{
			for(int w = 0; w < W; w++)
			{
				for(int i = 0; i < 4; i++)
				{
					int nh = h + dh[i];
					int nw = w + dw[i];
					if(nh < 0 || nh >= H || nw < 0 || nw >= W)
						continue;

					if(grid[h, w] == grid[nh, nw])
						continue;

					int v = h * W + w;
					int u = nh * W + nw;
					if(!uf.IsSame(v, u))
						uf.Merge(v, u);
				}
			}
		}

		var rootSet = new HashSet<int>();
		for(int h = 0; h < H; h++)
			for(int w = 0; w < W; w++)
				rootSet.Add(uf.Root(h * W + w));

		var rootList = rootSet.ToList();
		long ans = 0;
		foreach(int root in rootList)
			ans += (long)(uf.NumWhite(root) * uf.NumBlack(root));
		
		Console.WriteLine(ans);
	}
}


public class UnionFindEx
{
	private int[] par = null;
	private int[] rank = null;
	private int[] m_numWhite = null;
	private int[] m_numBlack = null;
	
	public UnionFindEx(int n, int[] numWhite, int[] numBlack)
	{
		m_numWhite = numWhite;
		m_numBlack = numBlack;

		par = new int[n];
		rank = new int[n];
		for(int i = 0; i < n; i++)
		{
			par[i] = i;
			rank[i] = 0;
		}
	}

	public bool IsSame(int x, int y)
	{
		return (Root(x) == Root(y));
	}

	public int Root(int x)
	{
		if(par[x] == x)
			return x;

		int r = Root(par[x]);
		par[x] = r;
		return r;
	}
	
	public bool Merge(int x, int y)
	{
		int rx = Root(x);
		int ry = Root(y);
		if(rx == ry)
			return false;
		
		if(rank[rx] < rank[ry])
		{
			int tmp = rx;
			rx = ry;
			ry = tmp;
		}
		else if(rank[rx] == rank[ry])
			rank[rx]++;

		par[ry] = rx;
		m_numWhite[rx] = m_numWhite[rx] + m_numWhite[ry];
		m_numBlack[rx] = m_numBlack[rx] + m_numBlack[ry];
		return true;
	}

	public int NumWhite(int x)
	{
		return m_numWhite[Root(x)];
	}

	public int NumBlack(int x)
	{
		return m_numBlack[Root(x)];
	}
}


public static class MyIO
{
	private static string[] args = null;
	private static int num = -1;
	private static int used = -1;

	private static string getArg()
	{
		if(used == num)
		{
			args = Console.ReadLine().Split(' ');
			num = args.Length;
			used = 0;
		}
		return args[used++];
	}

	public static int GetInt(){ return int.Parse(getArg()); }
	public static long GetLong(){ return long.Parse(getArg()); }
	public static double GetDouble(){ return double.Parse(getArg()); }
	public static string GetString(){ return getArg(); }
}