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 Rust (1.15.1) (judge verdict: Wrong Answer)

#[allow(unused_macros)]
macro_rules! scan {
    () => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.trim().to_string()
        }
    };
    (;;) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.trim().split_whitespace().map(|s| s.to_string()).collect::<Vec<String>>()
        }
    };
    (;;$n:expr) => {
        {
            (0..$n).map(|_| scan!()).collect::<Vec<_>>()
        }
    };
    ($t:ty) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t:ty),*) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            (
                $(iter.next().unwrap().parse::<$t>().unwrap(),)*
            )
        }
    };
    ($t:ty;;) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|t| t.parse::<$t>().unwrap())
                .collect::<Vec<_>>()
        }
    };
    ($t:ty;;$n:expr) => {
        (0..$n).map(|_| scan!($t;;)).collect::<Vec<_>>()
    };
    ($t:ty; $n:expr) => {
        (0..$n).map(|_|
                    scan!($t)
        ).collect::<Vec<_>>()
    };
    ($($t:ty),*; $n:expr) => {
        (0..$n).map(|_|
                    scan!($($t),*)
        ).collect::<Vec<_>>()
    };
}

fn dfs(graph: &[Vec<char>], colors: &mut [Vec<i64>], i: usize, j: usize, color: i64, bw: usize) {
    colors[i as usize][j as usize] = color;

    for &(k, l) in &[(1, 0), (-1, 0), (0, -1), (0, 1)] {
        let w = i as i64 + k;
        if !(0 <= w && w < graph.len() as i64) {
            continue;
        }
        let v = j as i64 + l;
        if !(0 <= v && v < graph[0].len() as i64) {
            continue;
        }

        let (x, y) = (w as usize, v as usize);
        if colors[x][y] == -1 {
            if bw == 0 {
                if graph[x][y] == '.' {
                    dfs(graph, colors, x, y, color, 1)
                }
            } else {
                if graph[x][y] == '#' {
                    dfs(graph, colors, x, y, color, 0)
                }
            }
        }
    }
}

fn main() {
    let (h, w) = scan!(usize, usize);
    let vs = scan!(String;h);

    let graph: Vec<_> = vs.iter().map(|v| v.chars().collect::<Vec<_>>()).collect();
    let mut colors = vec![vec![-1; w]; h];

    let mut color = 0i64;
    for i in 0..h {
        for j in 0..w {
            if colors[i][j] == -1 && graph[i][j] == '#' {
                dfs(&graph, &mut colors, i, j, color, 0);
                color += 1;
            }
        }
    }

    let mut whites = vec![0; color as usize + 1];
    let mut blacks = vec![0; color as usize + 1];
    for (i, l) in colors.iter().enumerate() {
        for (j, &c) in l.iter().enumerate() {
            if c == -1 {
                continue;
            }
            if graph[i][j] == '.' {
                whites[c as usize] += 1;
            } else {
                blacks[c as usize] += 1;
            }
        }
    }

    println!(
        "{}",
        whites
            .iter()
            .zip(blacks.iter())
            .map(|(w, b)| w * b)
            .sum::<i32>()
    )
}