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: Time Limit Exceeded)

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 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;
                    var stack = new Stack<Coordinate>();
                    stack.Push(new Coordinate(x, y));
                    if (color[y, x] == Color.B) { totalB++; }
                    else { totalW++; }

                    var dfsColor = new int[h, w];
                    dfsColor[y, x] = DFSGray;
                    while (stack.Count >= 1)
                    {
                        var u = stack.Peek();
                        Coordinate v = Next(u);
                       
                        if( v.x != -10)
                        {
                            
                            if(IsConnected(u,v, w, h, color) &&  dfsColor[v.y, v.x] == DFSWhite)
                            {
                                used[v.y, v.x] = true;
                                dfsColor[v.y, v.x] = DFSGray;
                                if (color[v.y, v.x] == Color.B) { totalB++; }
                                else { totalW++; }
                                stack.Push(v);
                            }
                        }
                        else
                        {
                            stack.Pop();
                            dfsColor[u.y, u.x] = DFSBlack;

                        }
                    }

                    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
    }
}