#[allow(unused_macros)]
macro_rules! scan {
    () => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.trim().to_string()
        }
    };
    (;;) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.trim().split_whitespace().map(|s| s.to_string()).collect::<Vec<String>>()
        }
    };
    (;;$n:expr) => {
        {
            (0..$n).map(|_| scan!()).collect::<Vec<_>>()
        }
    };
    ($t:ty) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t:ty),*) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            (
                $(iter.next().unwrap().parse::<$t>().unwrap(),)*
            )
        }
    };
    ($t:ty;;) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|t| t.parse::<$t>().unwrap())
                .collect::<Vec<_>>()
        }
    };
    ($t:ty;;$n:expr) => {
        (0..$n).map(|_| scan!($t;;)).collect::<Vec<_>>()
    };
    ($t:ty; $n:expr) => {
        (0..$n).map(|_|
                    scan!($t)
        ).collect::<Vec<_>>()
    };
    ($($t:ty),*; $n:expr) => {
        (0..$n).map(|_|
                    scan!($($t),*)
        ).collect::<Vec<_>>()
    };
}

fn dfs(graph: &[Vec<char>], colors: &mut [Vec<i64>], i: usize, j: usize, color: i64, bw: usize) {
    colors[i as usize][j as usize] = color;

    for &(k, l) in &[(1, 0), (-1, 0), (0, -1), (0, 1)] {
        let w = i as i64 + k;
        if !(0 <= w && w < graph.len() as i64) {
            continue;
        }
        let v = j as i64 + l;
        if !(0 <= v && v < graph[0].len() as i64) {
            continue;
        }

        let (x, y) = (w as usize, v as usize);
        if colors[x][y] == -1 {
            if bw == 0 {
                if graph[x][y] == '.' {
                    dfs(graph, colors, x, y, color, 1)
                }
            } else {
                if graph[x][y] == '#' {
                    dfs(graph, colors, x, y, color, 0)
                }
            }
        }
    }
}

fn main() {
    let (h, w) = scan!(usize, usize);
    let vs = scan!(String;h);

    let graph: Vec<_> = vs.iter().map(|v| v.chars().collect::<Vec<_>>()).collect();
    let mut colors = vec![vec![-1; w]; h];

    let mut color = 0i64;
    for i in 0..h {
        for j in 0..w {
            if colors[i][j] == -1 {
                dfs(
                    &graph,
                    &mut colors,
                    i,
                    j,
                    color,
                    if graph[i][j] == '#' { 0 } else { 1 },
                );
                color += 1;
            }
        }
    }

    let mut whites = vec![0i64; color as usize + 1];
    let mut blacks = vec![0i64; color as usize + 1];
    for (i, l) in colors.iter().enumerate() {
        for (j, &c) in l.iter().enumerate() {
            if graph[i][j] == '.' {
                whites[c as usize] += 1;
            } else {
                blacks[c as usize] += 1;
            }
        }
    }

    println!(
        "{}",
        whites
            .iter()
            .zip(blacks.iter())
            .map(|(w, b)| w * b)
            .sum::<i64>()
    )
}


Measured memory_bytes: 21372