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) = parse!(it, 2);
    let g: Vec<Vec<bool>> = (0..h)
        .map(|_| it.next().unwrap().chars().map(|c| c == '.').collect())
        .collect();
    let mut seen = std::collections::HashSet::<(usize, usize)>::new();
    let mut ret = 0;
    for i in 0..h {
        for j in 0..w {
            if seen.contains(&(i, j)) {
                continue;
            }
            let mut q = std::collections::VecDeque::<(usize, usize)>::new();
            q.push_back((i, j));
            seen.insert((i, j));
            let mut ca = 0;
            let mut cb = 0;
            while let Some((i, j)) = q.pop_front() {
                if g[i][j] {
                    ca += 1
                } else {
                    cb += 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.contains(&(ni, nj)) && g[ni][nj] != g[i][j] {
                        seen.insert((ni, nj));
                        q.push_back((ni, nj))
                    }
                }
            }
            ret += ca * cb;
        }
    }
    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);
    }
}