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));
}


Measured memory_bytes: 6396