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;
using System.Threading.Tasks;

namespace MyConsoleAppAlternatingPath4
{
    struct StartAndGoal
    {
        public int start_x;
        public int start_y;
        public int goal_x;
        public int goal_y;
    }

    class Program
    {
        static string[,] Board;
        static StartAndGoal sg;

        static List<StartAndGoal> ls1 = new List<StartAndGoal>();
        static List<StartAndGoal> ls2 = new List<StartAndGoal>();
        static List<StartAndGoal> ls3 = new List<StartAndGoal>();

        static void Main(string[] args)
        {
            // 整数の入力
            //int a = int.Parse(Console.ReadLine());
            // スペース区切りの整数の入力
            string[] input = Console.ReadLine().Split(' ');
            int hight = int.Parse(input[0]);//高さ
            int width = int.Parse(input[1]);//幅

            Board = new string[hight, width];

            // 文字列の入力
            for (int i = 0; i < hight; i++)
            {
                string s = Console.ReadLine();
                for (int j = 0; j < width; j++)
                {
                    string ss = s.Substring(j, 1);
                    Board[i, j] = ss;
                }
            }

            // "#"の周囲に"."がないか
            ls1.Clear();
            for (int i = 0; i < hight; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    if (Board[i, j] == "#")
                    {
                        CheckAround(i, j, hight, width);
                    }
                }
            }
            //
            //出力
            /*
            Console.WriteLine("ls1:"+ls1.Count);
            foreach(StartAndGoal element in ls1)
            {
                Console.WriteLine(element.start_x+" "+element.start_y+" "+element.goal_x+" "+element.goal_y);
            }
            */
            //
            ls2.Clear();
            ls3.Clear();
            int count_node; // ノードの数　新しいノードがなくなったら終了する
            do
            {
                count_node = ls3.Count;
                Combine(hight, width);
            } while (count_node != ls3.Count);

            //出力
            ls3.Clear();
            ls1.AddRange(ls2);
            ls3.AddRange(ls1.Distinct());

            Console.WriteLine(ls3.Count);
        }
        //メソッドCombine　二つのノードを結合する
        public static void Combine(int h, int w)
        {
            int temp_x;
            int temp_y;
            for (int i = 0; i < ls1.Count; i++)
            {
                if (ls1[i].goal_x > 0)
                {
                    temp_x = ls1[i].goal_x - 1;
                    temp_y = ls1[i].goal_y;
                    SetNodeLs2(i, temp_x, temp_y);
                }
                if (ls1[i].goal_x < (h - 1))
                {
                    temp_x = ls1[i].goal_x + 1;
                    temp_y = ls1[i].goal_y;
                    SetNodeLs2(i, temp_x, temp_y);
                }
                if (ls1[i].goal_y > 0)
                {
                    temp_x = ls1[i].goal_x;
                    temp_y = ls1[i].goal_y - 1;
                    SetNodeLs2(i, temp_x, temp_y);
                }
                if (ls1[i].goal_y < (w - 1))
                {
                    temp_x = ls1[i].goal_x;
                    temp_y = ls1[i].goal_y + 1;
                    //
                    SetNodeLs2(i,temp_x,temp_y);
                }

            }
        }
        // メソッド　新たなノードをリストls2に書き込む
        public static void SetNodeLs2(int startNode,int temp_X,int temp_Y)
        {
            for (int j = 0; j < ls1.Count; j++)
            {
                if (((ls1[j].start_x == temp_X) && (ls1[j].start_y == temp_Y)) && !(
                    (ls1[startNode].start_x == temp_X) && (ls1[startNode].start_y == temp_Y)) &&
                    !((ls1[j].goal_x == ls1[startNode].goal_x) && (ls1[j].goal_y == ls1[startNode].goal_y)))
                {
                    sg.start_x = ls1[startNode].start_x;
                    sg.start_y = ls1[startNode].start_y;
                    sg.goal_x = ls1[j].goal_x;
                    sg.goal_y = ls1[j].goal_y;
                    ls2.Add(sg);
                }
            }
        }
        //メソッドCheckAround SとGの組合せリストをつくる
        public static void CheckAround(int a, int b, int hh, int ww)
        {
            if (a > 0)
            {
                if (Board[a - 1, b] == ".")
                {
                    SetNodeLs1(a, b, a-1, b);
                }
            }
            if (a < (hh - 1))
            {
                if (Board[a + 1, b] == ".")
                {
                    SetNodeLs1(a, b, a+1, b);
                }
            }

            if (b > 0)
            {
                if (Board[a, b - 1] == ".")
                {
                    SetNodeLs1(a, b, a, b - 1);
                }
            }
            if (b < (ww - 1))
            {
                if (Board[a, b + 1] == ".")
                {
                    SetNodeLs1(a, b, a, b + 1);
                }
            }
        }
        // メソッド SetNodeLs1
        public static void SetNodeLs1(int start_X, int start_Y, int goal_X, int goal_Y)
        {
            sg.start_x = start_X;
            sg.start_y = start_Y;
            sg.goal_x = goal_X;
            sg.goal_y = goal_Y;
            ls1.Add(sg); //リストに追加する
        }
    }
}