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)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use union_find::*;
fn main() {
    proconio::input! {
        h: usize, w: usize,
        s: [proconio::marker::Bytes; h]
    }
    let mut uf: QuickUnionUf<BW> = UnionFind::new(h * w);
    for i in 0..h {
        for j in 0..w {
            *uf.get_mut(i * w + j) = if s[i][j] == b'#' { BW(1, 0) } else { BW(0, 1) }
        }
    }
    for i in 0..h {
        for j in 0..w {
            if i + 1 < h && s[i][j] != s[i + 1][j] {
                uf.union(i * w + j, (i + 1) * w + j);
            }
            if j + 1 < w && s[i][j] != s[i][j + 1] {
                uf.union(i * w + j, i * w + (j + 1));
            }
        }
    }
    let ans: u32 = (0..h * w)
        .map(|i| {
            if uf.find(i) == i {
                let &BW(b, w) = uf.get(i);
                b * w
            } else {
                0
            }
        })
        .sum();
    println!("{}", ans);
}

#[derive(Default)]
struct BW(u32, u32);

impl Union for BW {
    fn union(BW(lb, lw): Self, BW(rb, rw): Self) -> UnionResult<Self> {
        if lb + lw < rb + rw {
            UnionResult::Right(BW(lb + rb, lw + rw))
        } else {
            UnionResult::Left(BW(lb + rb, lw + rw))
        }
    }
}
*/

fn main() {
    let exe = "/tmp/bin7073DF90";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";