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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;

namespace AtCoder.C
{
    public class Program
    {
        public static long H { get; private set; }
        public static long W { get; private set; }
        public static bool[,] IsBlack { get; private set; }
        public static bool[,] IsReached { get; private set; }

        public static void Main() { var r = GetResult(); Debug.WriteLine(r); Console.Write(r); }

        private static object GetResult()
        {
            (H, W) = Read2Longs();
            IsBlack = new bool[H, W];
            // 到達済みかどうかを管理する
            IsReached = new bool[H, W];
            for (int i = 0; i < H; i++)
            {
                var S = ReadText();
                for (int j = 0; j < S.Length; j++)
                {
                    IsBlack[i, j] = S[j] == '#';
                }
            }

            var count = 0L;
            for (int i = 0; i < H; i++)
            {
                for (int j = 0; j < W; j++)
                {
                    if (IsReached[i, j]) continue;
                    IsReached[i, j] = true;
                    var reachable = GetReachableTable(i, j).ToList();
                    count += GetPathCount(reachable);
                }
            }

            return count;
        }

        private static IEnumerable<Point> GetReachableTable(int i, int j)
        {
            var start = new Point(i, j);
            yield return start;

            var queue = new Queue<Point>();
            var next_right = new Point(i + 1, j);
            if (CanBePath(start, next_right)) queue.Enqueue(next_right);
            var next_down = new Point(i, j + 1);
            if (CanBePath(start, next_down)) queue.Enqueue(next_down);

            while (queue.Count > 0)
            {
                var p = queue.Dequeue();
                yield return p;

                var right = new Point(p.H + 1, p.W);
                if (CanBePath(p, right)) queue.Enqueue(right);
                var left = new Point(p.H - 1, p.W);
                if (CanBePath(p, left)) queue.Enqueue(left);
                var down = new Point(p.H, p.W + 1);
                if (CanBePath(p, down)) queue.Enqueue(down);
                var up = new Point(p.H, p.W - 1);
                if (CanBePath(p, up)) queue.Enqueue(up);
            }
        }

        private static bool CanBePath(Point current, Point next)
        {
            if (next.H < 0 || next.H >= H) return false;
            if (next.W < 0 || next.W >= W) return false;
            if (IsBlackPoint(current) == IsBlackPoint(next)) return false;
            if (IsReached[next.H, next.W]) return false;

            IsReached[next.H, next.W] = true;
            return true;
        }

        private static bool IsBlackPoint(Point p)
        {
            return IsBlack[p.H, p.W];
        }
        private static long GetPathCount(List<Point> reachable)
        {
            var black = 0L;
            var white = 0L;
            foreach (var point in reachable)
            {
                if (IsBlackPoint(point))
                {
                    black++;
                }
                else
                {
                    white++;
                }
            }

            return black * white;
        }

        class Point
        {
            public long H { get; set; }
            public long W { get; set; }

            public Point(long h, long w)
            {
                H = h;
                W = w;
            }
        }

        #region Console.Read

        public static string ReadText() { return Console.ReadLine(); }
        public static List<string> ReadTexts() { return Console.ReadLine().Split(' ').ToList(); }
        public static long ReadLong() { return long.Parse(Console.ReadLine()); }
        public static (long, long) Read2Longs() { var ls = ReadLongs(); return (ls[0], ls[1]); }
        public static (long, long, long) Read3Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2]); }
        public static (long, long, long, long) Read4Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2], ls[3]); }
        public static (long, long, long, long, long) Read5Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2], ls[3], ls[4]); }
        public static List<long> ReadLongs() { return Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToList(); }

        #endregion
    }
}