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 Java (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.*;
import java.io.*;
 
public class Main {
    static char[][] map;
    static boolean[][] arrived;
    public static void main(String[] args) throws Exception {
        FastScanner sc = new FastScanner(System.in);
        int h = sc.nextInt();
        int w = sc.nextInt();
        map = new char[h][w];
        for(int i = 0; i < h; i++){
            map[i] = sc.next().toCharArray();
        }
        arrived = new boolean[h][w];
        long ans = 0;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(!arrived[i][j] && map[i][j] == '.'){
                   ans += bfs(i, j, h, w);
                }
            }
        }
        System.out.println(ans);
    }
    
    public static int bfs(int i, int j, int h, int w){
        int ret = 0;
        int now = 0;
        int dot = 1;
        int sharp = 0;
        int[] my = {-1,0,0,1};
        int[] mx = {0,-1,1,0};
        Deque<Panel> dq = new ArrayDeque<Panel>();
        dq.add(new Panel(i,j));
        arrived[i][j] = true;
        while(dq.size() > 0){
            Panel p = dq.poll();
            int y = p.y;
            int x = p.x;
            for(int ii = 0; ii < 4; ii++){
                int ny = y + my[ii];
                int nx = x + mx[ii];
                if(checkPanel(ny,nx,h,w) && !arrived[ny][nx] && map[y][x] != map[ny][nx]){
                    arrived[ny][nx] = true;
                    dq.add(new Panel(ny,nx));
                    if((Math.abs(i-ny)+Math.abs(j-nx)) % 2 == 1){
                        sharp++;
                    }else{
                        dot++;
                    }
                }
            }
        }
        return sharp*dot;
    }
    
    public static boolean checkPanel(int y, int x, int h, int w){
        return (0 <= y && y < h && 0 <= x && x < w);
    }
}

class Panel{
    int x,y;
    public Panel(int y, int x){
        this.x = x;
        this.y = y;
    }
}

class FastScanner {
    private BufferedReader reader = null;
    private StringTokenizer tokenizer = null;
    public FastScanner(InputStream in) {
        reader = new BufferedReader(new InputStreamReader(in));
        tokenizer = null;
    }

    public String next() {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public String nextLine() {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                return reader.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken("\n");
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public double nextDouble() {
         return Double.parseDouble(next());
    }
    
    public String[] nextArray(int n) {
        String[] a = new String[n];
        for (int i = 0; i < n; i++)
            a[i] = next();
        return a;
    }

    public int[] nextIntArray(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nextInt();
        return a;
    }

    public long[] nextLongArray(int n) {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nextLong();
        return a;
    } 
}