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: Accepted)

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

use std::collections::VecDeque;



fn bfs(h: i64, w: i64, s: Vec<Vec<bool>>) -> usize {
    let mut visited = vec![vec![false; w as usize]; h as usize];
    let mut res = 0;
    let next = vec![(0, -1), (-1, 0), (0, 1), (1, 0)];
    let mut q = VecDeque::new();
    for i in 0..h {
        for j in 0..w {
            if visited[i as usize][j as usize] {
                continue;
            }
            if s[i as usize][j as usize] {
                visited[i as usize][j as usize] = true;
                q.push_back(((i, j), true));
            }
            let mut black = 1;
            let mut white = 0;
            while let Some(((height, width), c)) = q.pop_front() {
                for n in 0..4 {
                    let n = next[n];
                    let height = height + n.1;
                    let width = width + n.0;
                    if width < 0 || height < 0 || width >= w || height >= h {
                        continue;
                    }
                    if visited[height as usize][width as usize] {
                        continue;
                    }
                    if c ^ s[height as usize][width as usize] {
                        q.push_back(((height, width), s[height as usize][width as usize]));
                        if s[height as usize][width as usize] {
                            black += 1;
                        } else {
                            white += 1;
                        }
                        visited[height as usize][width as usize] = true;
                    }
                }
            }
            res += black * white;
        }
    }
    res
}


// fn solve(n: usize, edges: Vec<vec<char>) -> usize {
// }

fn main() {
    input!{
        h: usize,
        w: usize,
        s: [chars; h],
    }
    let mut ss = Vec::new();
    for i in 0..h {
        let mut row = Vec::new();
        for j in 0..w {
            if s[i][j] == '.' {
                row.push(false);
            } else {
                row.push(true);
            }
        }
        ss.push(row);
    }

    println!("{}", bfs(h as i64, w as i64, ss));
}