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;

namespace C
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var input = GetInput();
            Console.WriteLine(Solve(input));
        }

        private static int Solve(Dictionary<Position, int> dic)
        {
            var reachableDic = new Dictionary<Position, Dictionary<Position, bool>>();
            foreach (Position pos in dic.Keys)
            {
                reachableDic[pos] = new Dictionary<Position, bool>();
            }

            int ret = 0;
            foreach (Position p1 in dic.Keys)
            {
                if (dic[p1] == -1)
                {
                    foreach (Position p2 in dic.Keys)
                    {
                        if (dic[p2] == 1)
                        {
                            if (reachable(p1, p2, dic, reachableDic)) { ret++; }

                        }
                    }
                }
            }
            return ret;
        }

        private static bool reachable(Position start, Position goal, Dictionary<Position, int> dic, Dictionary<Position, Dictionary<Position, bool>> reachableDic)
        {
            if (reachableDic[start].ContainsKey(goal))
            {
                return reachableDic[start][goal];
            }
            else
            {

                Stack<Tuple<Position, int>> stack = new Stack<Tuple<Position, int>>();
                stack.Push(Tuple.Create(start, dic[start]));
                while (stack.Count > 0)
                {
                    Tuple<Position, int> tp = stack.Pop();
                    Position pos = tp.Item1;
                    int color = tp.Item2;
                    foreach (Position np in pos.Neighbors)
                    {
                        if (dic.ContainsKey(np))
                        {
                            if (!reachableDic[start].ContainsKey(np))
                            {
                                if (reachableDic[start][np] = color != dic[np])
                                {
                                    reachableDic[start][np] = true;

                                    stack.Push(Tuple.Create(np, dic[np]));
                                }
                            }
                        }
                    }
                }



                if (!reachableDic[start].ContainsKey(goal))
                {
                    reachableDic[start][goal] = false;
                }
                return reachableDic[start][goal];


            }


        }

        private static Dictionary<Position, int> GetInput()
        {
            Dictionary<Position, int> ret = new Dictionary<Position, int>();
            string[] items = Console.ReadLine().Split(' ');
            int h = int.Parse(items[0]);
            int w = int.Parse(items[1]);
            for (int y = 0; y < h; y++)
            {
                string line = Console.ReadLine();
                for (int x = 0; x < w; x++)
                {
                    if (line[x] == '#')
                    {
                        ret[new Position(x, y)] = 1;
                    }
                    else
                    {
                        ret[new Position(x, y)] = -1;
                    }
                }
            }
            return ret;
        }

        private struct Position
        {
            public readonly int X, Y;
            public Position(int x, int y)
            {
                X = x;
                Y = y;
            }
            public override int GetHashCode()
            {
                return X + Y * 1000;
            }
            public override bool Equals(object obj)
            {
                return obj is Position && ((Position)obj).X == X && ((Position)obj).Y == Y;
            }

            public IEnumerable<Position> Neighbors
            {
                get
                {
                    yield return new Position(X - 1, Y);
                    yield return new Position(X + 1, Y);
                    yield return new Position(X, Y - 1);
                    yield return new Position(X, Y + 1);
                }
            }
        }

    }
}