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;
                    for (int j = 0; j < ls1.Count; j++)
                    {
                        if (((ls1[j].start_x == temp_x) && (ls1[j].start_y == temp_y)) && !(
                            (ls1[i].start_x == temp_x) && (ls1[i].start_y == temp_y)) &&
                            !((ls1[j].goal_x == ls1[i].goal_x) && (ls1[j].goal_y == ls1[i].goal_y)))
                        {
                            sg.start_x = ls1[i].start_x;
                            sg.start_y = ls1[i].start_y;
                            sg.goal_x = ls1[j].goal_x;
                            sg.goal_y = ls1[j].goal_y;
                            ls2.Add(sg);
                        }
                    }
                }
                if (ls1[i].goal_x < (h - 1))
                {
                    temp_x = ls1[i].goal_x + 1;
                    temp_y = ls1[i].goal_y;
                    for (int j = 0; j < ls1.Count; j++)
                    {
                        if (((ls1[j].start_x == temp_x) && (ls1[j].start_y == temp_y)) && !(
                            (ls1[i].start_x == temp_x) && (ls1[i].start_y == temp_y)) &&
                            !((ls1[j].goal_x == ls1[i].goal_x) && (ls1[j].goal_y == ls1[i].goal_y)))
                        {
                            sg.start_x = ls1[i].start_x;
                            sg.start_y = ls1[i].start_y;
                            sg.goal_x = ls1[j].goal_x;
                            sg.goal_y = ls1[j].goal_y;
                            ls2.Add(sg);
                        }
                    }
                }
                if (ls1[i].goal_y > 0)
                {
                    temp_x = ls1[i].goal_x;
                    temp_y = ls1[i].goal_y - 1;
                    for (int j = 0; j < ls1.Count; j++)
                    {
                        if (((ls1[j].start_x == temp_x) && (ls1[j].start_y == temp_y)) && !(
                            (ls1[i].start_x == temp_x) && (ls1[i].start_y == temp_y)) &&
                            !((ls1[j].goal_x == ls1[i].goal_x) && (ls1[j].goal_y == ls1[i].goal_y)))
                        {
                            sg.start_x = ls1[i].start_x;
                            sg.start_y = ls1[i].start_y;
                            sg.goal_x = ls1[j].goal_x;
                            sg.goal_y = ls1[j].goal_y;
                            ls2.Add(sg);
                        }
                    }

                }
                if (ls1[i].goal_y < (w - 1))
                {
                    temp_x = ls1[i].goal_x;
                    temp_y = ls1[i].goal_y + 1;
                    for (int j = 0; j < ls1.Count; j++)
                    {
                        if (((ls1[j].start_x == temp_x) && (ls1[j].start_y == temp_y)) && !(
                            (ls1[i].start_x == temp_x) && (ls1[i].start_y == temp_y)) &&
                            !((ls1[j].goal_x == ls1[i].goal_x) && (ls1[j].goal_y == ls1[i].goal_y)))
                        {
                            sg.start_x = ls1[i].start_x;
                            sg.start_y = ls1[i].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] == ".")
                {
                    sg.start_x = a;
                    sg.start_y = b;
                    sg.goal_x = a - 1;
                    sg.goal_y = b;
                    ls1.Add(sg); //リストに追加する
                }
            }
            if (a < (hh - 1))
            {
                if (Board[a + 1, b] == ".")
                {
                    sg.start_x = a;
                    sg.start_y = b;
                    sg.goal_x = a + 1;
                    sg.goal_y = b;
                    ls1.Add(sg); //リストに追加する
                }
            }

            if (b > 0)
            {
                if (Board[a, b - 1] == ".")
                {
                    sg.start_x = a;
                    sg.start_y = b;
                    sg.goal_x = a;
                    sg.goal_y = b - 1;
                    ls1.Add(sg); //リストに追加する
                }
            }
            if (b < (ww - 1))
            {
                if (Board[a, b + 1] == ".")
                {
                    sg.start_x = a;
                    sg.start_y = b;
                    sg.goal_x = a;
                    sg.goal_y = b + 1;
                    ls1.Add(sg); //リストに追加する
                }
            }
        }
    }
}