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 proconio::input;
#[allow(unused_imports)]
use proconio::marker::*;
#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
#[allow(unused_imports)]
use std::f64::consts::*;

#[allow(unused)]
const INF: usize = std::usize::MAX / 4;
#[allow(unused)]
const M: usize = 1000000007;

fn main() {
    input! {
        h: usize,
        w: usize,
        s: [Chars; h],
    }
    let n = h * w;
    let mut graph = vec![vec![]; n];
    for i in 0..h {
        for j in 1..w {
            if s[i][j - 1] != s[i][j] {
                graph[w * i + j - 1].push(w * i + j);
                graph[w * i + j].push(w * i + j - 1);
            }
        }
    }
    for i in 1..h {
        for j in 0..w {
            if s[i - 1][j] != s[i][j] {
                graph[w * (i - 1) + j].push(w * i + j);
                graph[w * i + j].push(w * (i - 1) + j);
            }
        }
    }

    let mut components = vec![INF; n];
    for w in 0..n {
        if components[w] != INF {
            continue;
        }
        let mut queue = VecDeque::new();
        queue.push_back(w);
        while let Some(u) = queue.pop_front() {
            if components[u] != INF {
                continue;
            }
            components[u] = w;
            for &v in &graph[u] {
                queue.push_back(v);
            }
        }
    }

    let mut component_nodes = vec![vec![]; n];
    for u in 0..n {
        component_nodes[components[u]].push(u);
    }

    let mut result = 0;
    for c in 0..n {
        let mut black = 0;
        let mut white = 0;
        for &u in &component_nodes[c] {
            if s[u / w][u % w] == '#' {
                black += 1;
            } else {
                white += 1;
            }
        }
        result += black * white;
    }
    println!("{}", result);
}
*/

fn main() {
    let exe = "/tmp/bin0B237CFD";
    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>";