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.*;

public class Main {
    static int h, w;
    static int[][] map;
    static boolean[][] bmap;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        h = sc.nextInt();
        w = sc.nextInt();
        map = new int[h+2][w+2];
        bmap = new boolean[h+2][w+2];
        for(int i=0; i<h; i++){
            String s = sc.next();
            for(int j=0; j<w; j++){
                if(s.charAt(j) == '#'){
                    map[i+1][j+1] = 1;
                }else{
                    map[i+1][j+1] = -1;
                }
            }
        }
        sc.close();

        long ans = 0;
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                if(!bmap[i+1][j+1] && map[i+1][j+1] == 1){
                    int[] ret = bfs(i+1, j+1, map[i+1][j+1]);
                    ans += (long)ret[0] * (long)ret[1];
                    // ans += cont * (cont-1) * 2;
                }
            }
        }

        System.out.println(ans);
    }

    public static int[] bfs(int x, int y, int now){
        int next = -now;
        int[] ret = new int[2];
        ret[0] = map[x][y] == -1 ? 1 : 0;
        ret[1] = map[x][y] == 1 ? 1 : 0;
        bmap[x][y] = true;
        if(!bmap[x+1][y] && map[x+1][y] == next){
            int[] mret = bfs(x+1, y, next);
            ret[0] += mret[0];
            ret[1] += mret[1];
        }
        if(!bmap[x-1][y] && map[x-1][y] == next){
            int[] mret = bfs(x-1, y, next);
            ret[0] += mret[0];
            ret[1] += mret[1];
        }
        if(!bmap[x][y+1] && map[x][y+1] == next){
            int[] mret = bfs(x, y+1, next);
            ret[0] += mret[0];
            ret[1] += mret[1];
        }
        if(!bmap[x][y-1] && map[x][y-1] == next){
            int[] mret = bfs(x, y-1, next);
            ret[0] += mret[0];
            ret[1] += mret[1];
        }
        return ret;
    }
}