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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)



import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Main {
    public static class UnionFindTree {
        // nodeNum 个数，下标 [1, nodeNum]
        int nodeNum;
        int [] parent;
        int [] height;

        public UnionFindTree(int nodeNum) {
            this.nodeNum = nodeNum;
            parent = new int[nodeNum + 1];
            height = new int[nodeNum + 1];
            for (int i = 1; i <= nodeNum; ++i) {
                parent[i] = i;
                height[i] = 0;
            }
        }

        int find(int x) {
            if (parent[x] == x) {
                return x;
            } else {
                return parent[x] = find(parent[x]);
            }
        }

        void union(int x, int y) {
            int rootX = find(x);
            int rootY = find(y);
            if (rootX == rootY) {
                return;
            }
            if (height[rootX] < height[rootY]) {
                parent[rootX] = rootY;
            } else {
                if (height[rootX] == height[rootY]) {
                    height[rootX]++;
                }
                parent[rootY] = rootX;
            }
        }

    }
    public static void main(String [] args) {
        Scanner scanner = new Scanner(System.in);
        int h = scanner.nextInt();
        int w = scanner.nextInt();
        char[][] graph = new char[h][w];
        for (int i = 0; i < h; ++i) {
            String s = scanner.next();
            for (int j = 0; j < w; ++j) {
                graph[i][j] = s.charAt(j);
            }
        }
        UnionFindTree unionFindTree = new UnionFindTree(h * w);
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                if (i + 1 < h) {
                    if (graph[i][j] != graph[i + 1][j]) {
                        unionFindTree.union(
                                i * w + j,
                                (i + 1) * w + j
                        );
                    }
                }
                if (i - 1 >= 0) {
                    if (graph[i][j] != graph[i - 1][j]) {
                        unionFindTree.union(
                                i * w + j,
                                (i - 1) * w + j
                        );
                    }
                }
                if (j + 1 < w) {
                    if (graph[i][j] != graph[i][j + 1]) {
                        unionFindTree.union(
                                i * w + j,
                                i * w + j + 1
                        );
                    }
                }

                if (j - 1 >= 0) {
                    if (graph[i][j] != graph[i][j - 1]) {
                        unionFindTree.union(
                                i * w + j,
                                i * w + j - 1
                        );
                    }
                }

            }
        }
        Map<Integer, Set<Integer>> blacks = new HashMap<>();
        Map<Integer, Set<Integer>> whites = new HashMap<>();
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                int root = unionFindTree.find(i * w + j);

                if (graph[i][j] == '#') {
                    Set<Integer> node = blacks.getOrDefault(root, new HashSet<>());
                    node.add(i * w + j);
                    blacks.put(root, node);
                } else {
                    Set<Integer> node = whites.getOrDefault(root, new HashSet<>());
                    node.add(i * w + j);
                    whites.put(root, node);
                }
            }
        }
        long result = 0;
        for (Map.Entry<Integer, Set<Integer>> rootAndSize : whites.entrySet()) {
            int blackNum = blacks.getOrDefault(rootAndSize.getKey(), new HashSet<>()).size();
            int whiteNum = rootAndSize.getValue().size();
            result += (long)blackNum * (long)whiteNum;
        }
        System.out.println(result);


    }
}