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)

use std::fs;
use std::io::{stdin, Read};

macro_rules! parse {
    ($it: ident) => {
        $it.next().unwrap().parse().unwrap()
    };
    ($it: ident,2) => {
        (parse!($it), parse!($it))
    };
    ($it: ident,3) => {
        (parse!($it), parse!($it), parse!($it))
    };
    ($it: ident,4) => {
        (parse!($it), parse!($it), parse!($it), parse!($it))
    };
    ($it: ident, $n: expr) => {
        (&mut $it).take($n).map(|e| e.parse().unwrap()).collect();
    };
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    let (h, w): (usize, usize) = parse!(it, 2);
    let mut g: Vec<Vec<bool>> = vec![];
    for _ in 0..h {
        let s = it.next().unwrap();
        g.push(s.chars().map(|c| c == '#').collect());
    }
    let mut seen = vec![vec![false; w]; h];
    let mut ret = 0;
    for i in 0..h {
        for j in 0..w {
            if seen[i][j] {
                continue;
            }
            let mut stack = vec![(i, j)];
            seen[i][j] = true;
            let mut sa = 0;
            let mut sb = 0;
            loop {
                if stack.len() == 0 {
                    break;
                }
                let (i, j) = stack.pop().unwrap();
                if g[i][j] {
                    sa += 1;
                } else {
                    sb += 1;
                }
                let mut nexts = vec![];
                if i > 0 {
                    nexts.push((i - 1, j));
                }
                if j > 0 {
                    nexts.push((i, j - 1));
                }
                if i < h - 1 {
                    nexts.push((i + 1, j));
                }
                if j < w - 1 {
                    nexts.push((i, j + 1));
                }
                for (ni, nj) in nexts {
                    if !seen[ni][nj] && g[i][j] != g[ni][nj] {
                        stack.push((ni, nj));
                        seen[ni][nj] = true;
                    }
                }
            }
            ret += sa * sb;
        }
    }
    println!("{}", ret);
}

fn test() {
    let mut f = fs::File::open("foo.txt").unwrap();
    let mut s = String::new();
    f.read_to_string(&mut s).unwrap();
    solve(&s);
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_main() {
        test();
    }
    #[test]
    fn test_input() {
        let s = "
        ";
        solve(s);
    }
}