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

namespace Practice
{
    class Program
    {
        static void Main(string[] args)
        {
            Solve();
        }
        private const int MOD = 1000000007;
        private enum Color
        {
            B,
            W,
        }
        private struct Coordinate
        {
            public int x;
            public int y;
            public Coordinate(int x, int y)
            {
                this.x
                     = x;
                this.y = y;
            }
        }
        private const int DFSWhite = 0;
        private const int DFSGray = 1;
        private const int DFSBlack = 2;
        private static readonly int[] offsetX = { 0, -1, 1, 0 };// 2468方向
        private static readonly int[] offsetY = { 1, 0, 0, -1 };
        private static readonly Dictionary<Coordinate, int> nextTable = new Dictionary<Coordinate, int>();
        static void Solve()
        {
            var hw = ReadAndParseIntArr();
            int h = hw[0];
            int w = hw[1];
            var color = new Color[h, w];
            for (int i = 0; i < h; ++i)
            {
                var s = Console.ReadLine();
                for (int x = 0; x < w; ++x)
                {
                    color[i, x] = s[x] == '#' ? Color.B : Color.W;
                }
            }
            long ans = 0;
            var queue = new Queue<Coordinate>();
            var dfsColor = new int[h, w];

            var used = new bool[h, w];
            for (int x = 0; x < w; ++x)
            {
                for (int y = 0; y < h; ++y)
                {
                    if (used[y, x]) { continue; }

                    used[y, x] = true;
                    // dfs
                    int totalB = 0;
                    int totalW = 0;
                    queue.Clear();
                    queue.Enqueue(new Coordinate(x, y));
                    if (color[y, x] == Color.B) { totalB++; }
                    else { totalW++; }


                    dfsColor[y, x] = DFSGray;
                    while (queue.Count >= 1)
                    {
                        var u = queue.Peek();
                        queue.Dequeue();
                        for(int d = 0;d<4;++d)
                        {
                            var v = new Coordinate(u.x + offsetX[d], u.y + offsetY[d]);
                            if(!IsConnected(u, v, w, h, color)){ continue; }
                            if(used[v.y, v.x]) { continue; }
                            used[v.y, v.x] = true;
                            if (color[v.y, v.x] == Color.B) { totalB++; }
                            else { totalW++; }

                            queue.Enqueue(v);
                        }
                     
                    }

                    ans += totalB * totalW;
                }
            }
            Console.WriteLine(ans);
        }
        private static Coordinate Next(Coordinate u)
        {
            if (!nextTable.ContainsKey(u))
            {
                nextTable[u] = 0;
            }
            int d = nextTable[u];
            nextTable[u]++;
            if (d <= 3)
            {
                return new Coordinate(u.x + offsetX[d], u.y + offsetY[d]);
            }
            else
            {
                return new Coordinate(-10, -10);
            }

        }
        private static bool IsConnected(Coordinate src, Coordinate dst, int w, int h, Color[,] table)
        {
            if (dst.x < 0 || w <= dst.x) { return false; }
            if (dst.y < 0 || h <= dst.y) { return false; }
            if (table[src.y, src.x] == table[dst.y, dst.x]) { return false; }
            return true;
        }
        #region よく使う便利関数
        private static bool isPrime(long x)
        {
            if (x == 2) { return true; }
            if (x < 2 || x % 2 == 0) { return false; }
            long i = 3;
            while (i * i <= x)
            {
                if (x % i == 0) { return false; }
                i = i + 2;
            }
            return true;
        }
        private static long lcm(long x, long y)
        {
            return x / gcd(x, y) * y;
        }
        private static long gcd(long x, long y)
        {
            return y > 0 ? gcd(y, x % y) : x;
        }
        private static long pow(long x, long n)
        {
            if (n == 0) { return 1; }
            long res = pow(x * x % MOD, n / 2);
            if (n % 2 == 1)
            {
                res = res * x % MOD;
            }
            return res;
        }
        private static int ReadAndParseInt()
        {
            return int.Parse(Console.ReadLine());
        }
        private static int[] ReadAndParseIntArr()
        {
            return Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
        }
        private static long ReadAndParseLong()
        {
            return long.Parse(Console.ReadLine());
        }
        private static long[] ReadAndParseLongArr()
        {
            return Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse);
        }
        #endregion
    }
}