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: Wrong Answer)

import java.io.*;
import java.util.*;
public class Main {
	static int id=1;
	public static void main(String[] args) {
	
		MyScanner sc = new MyScanner();
		int h=sc.nextInt();
		int w=sc.nextInt();
		String  s[]=new String[h];
		for (int k=0;k<h;k++) {
			s[k]=sc.next();
		}
		int imf[][]=new int[h][w];
		for(int x=0;x<h;x++) {
			for(int y=0;y<w;y++) {
				imf[x][y]=0;
			} //初期化
		}	
		int num[][]=new int[160000][2];
		for(int i=0;i<h;i++) {
			for(int j=0;j<w;j++) {
				if(imf[i][j]==0) {
					create(num,imf,s,i,j,h,w,id);
				}
			}
		}
		// グループ化終了
		long ans=0;
		for (int a=1;a<=id;a++) {
			ans+=num[a][0]*num[a][1];
			
		}
		System.out.println(ans);
		}
		
	public static void create(int[][] num,int[][] imf,String[] s,int i,int j,int h,int w,int id) {
		if(imf[i][j]==0) {
			if(s[i].charAt(j)=='#') {
				num[id][0]++;
				
			}
			else {
				num[id][1]++;
				
			}
		}
		imf[i][j]=id;
		
		if(j!=w-1||i!=h-1) {
			if(j!=w-1) {
				if (s[i].charAt(j+1)!=s[i].charAt(j)) {
					create(num,imf,s,i,j+1,h,w,id);
					
				}
			}	
			if(i!=h-1) {
				if (s[i+1].charAt(j)!=s[i].charAt(j)) {
					create(num,imf,s,i+1,j,h,w,id);
				
				}
			}
		}
		else {
			plus();
			
		}
	}
	public static void plus() {
		id++;
	}
	public static PrintWriter out;

	public static class MyScanner {
		BufferedReader br;
		StringTokenizer st;

		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

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

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

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

	}
	

}