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

namespace Atcoder
{

    class Program
    {
        static void Main(string[] args)
        {
            int[] HW = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            string[][] S = new string[HW[0] ][];
            for (int i = 0; i < HW[0]; i++)
            {
                S[i] = new string[HW[1]];
                S[i] = Console.ReadLine().ToArray().Select(x=>x.ToString()).ToArray();

            }
            ;


            int output = 0;
            //全#点を出す
            
            for (int i = 0; i < HW[0]; i++)
            {
                for (int j = 0; j < HW[1]; j++)
                {
                    if (S[i][j] == "#")
                    {
                        string[][] map = new string[HW[0]][];
                        for (int k = 0; k< HW[0]; k++)
                        {
                            map[k] = new string[HW[1]];
                        }
                        output += number_movepoint(i, j, S, map, HW[0], HW[1]);
                    }
                }

            }
            Console.WriteLine(output);
        }

        public struct pair
        {
            public int x;
            public int y;
            public pair(int x,int y) {
                this.x = x;
                this.y = y;

            }

        }

        public static int number_movepoint(int x, int y, string[][] S,string[][] map,int H ,int W)
        {
            int output = 0;
            map[x][y] = ".";
            Queue<pair> q = new Queue<pair>();
            if (isSuitablePoint(x - 1, y, H, W))
            {
                if (map[x - 1][y] == null && S[x - 1][y] == ".")
                {
                    q.Enqueue(new pair(x - 1, y));
                    output++;
                    map[x-1][y] = ".";
                }
            }
            if (isSuitablePoint(x + 1, y, H, W))
            {
                if (map[x + 1][y] == null && S[x + 1][y] == ".")
                {
                    q.Enqueue(new pair(x + 1, y));
                    output++;
                    map[x+1][y] = ".";
                }
            }
            if (isSuitablePoint(x, y-1, H, W))
            {
                if (map[x][y-1] == null && S[x][y - 1] == ".")
                {
                    q.Enqueue(new pair(x, y-1));
                    output++;
                    map[x][y-1] = ".";
                }
            }
            if (isSuitablePoint(x, y+1, H, W))
            {
                if (map[x][y+1] == null && S[x][y + 1] == ".")
                {
                    q.Enqueue(new pair(x, y+1));
                    output++;
                    map[x][y+1] = ".";
                }
            }
            while (q.Count != 0)
            {
                pair temp = q.Dequeue();
                map[temp.x][temp.y] = ".";
                if (isSuitablePoint(temp.x - 1, temp.y, H, W))
                {
                    if (map[temp.x - 1][temp.y] == null && S[temp.x - 1][temp.y] != S[temp.x][temp.y])
                    {
                        q.Enqueue(new pair(temp.x - 1, temp.y));
                        map[temp.x - 1][temp.y] = ".";
                        if (S[temp.x-1][temp.y] == ".")
                        {
                            output++;

                        }
                    }
                }
                if (isSuitablePoint(temp.x + 1, temp.y, H, W))
                {
                    if (map[temp.x + 1][temp.y] == null && S[temp.x + 1][temp.y] != S[temp.x][temp.y])
                    {
                        q.Enqueue(new pair(temp.x + 1, temp.y));
                        map[temp.x + 1][temp.y] = ".";
                        if (S[temp.x+1][temp.y] == ".") {
                            output++;
                           
                        }
                    }
                }
                if (isSuitablePoint(temp.x, temp.y - 1, H, W))
                {
                    if (map[temp.x][temp.y - 1] == null && S[temp.x][temp.y - 1] != S[temp.x][temp.y])
                    {
                        q.Enqueue(new pair(temp.x, temp.y - 1));
                        map[temp.x][temp.y-1] = ".";
                        if (S[temp.x][temp.y-1] == ".") { output++;
                           
                        }
                    }
                }
                if (isSuitablePoint(temp.x, temp.y + 1, H, W))
                {
                    if (map[temp.x][temp.y + 1] == null && S[temp.x][temp.y + 1] != S[temp.x][temp.y])
                    {
                        q.Enqueue(new pair(temp.x, temp.y + 1));
                        map[temp.x ][temp.y+1] = ".";
                        if (S[temp.x][temp.y+1] == ".") {
                            output++;
                           
                        }
                    }
                }

            }

            return output;
        }

        public static bool isSuitablePoint(int x, int y,int H,int W)
        {
            if(x>=0 && y>= 0 && x<H&&y<W)
            {
                return true;

            }
            return false;
        }
    }
}