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 = vec![vec![false; w]; h];
    let mut ret = 0;
    let di = vec![1, -1, 0, 0];
    let dj = vec![0, 0, 1, -1];
    let around = |i: usize, j: usize| {
        let mut ret = vec![];
        for k in 0..4 {
            let ni = (i as isize) + di[k];
            let nj = (j as isize) + dj[k];
            if ni >= 0 && ni < (h as isize) && nj >= 0 && nj < (w as isize) {
                let ni = ni as usize;
                let nj = nj as usize;
                if g[i][j] != g[ni][nj] {
                    ret.push((ni, nj))
                }
            }
        }
        ret
    };
    for i in 0..h {
        for j in 0..w {
            if seen[i][j] {
                continue;
            }
            let mut q = std::collections::VecDeque::<(usize, usize)>::new();
            q.push_back((i, j));
            seen[i][j] = true;
            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
                }
                for (ni, nj) in around(i, j) {
                    if !seen[ni][nj] {
                        seen[ni][nj] = true;
                        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);
    }
}