Scanf.scanf "%d %d" (fun h w ->
    let s = Array.init h (fun _ -> Scanf.scanf " %s" (fun s -> s)) in
    let hw = h * w in
    let par = Array.init hw (fun i -> i) in

    let rec root x =
        if x = par.(x) then par.(x) else
            let r = root par.(x) in
            let () = par.(x) <- r in
            r
    in

    let merge a b = if root a <> root b then par.(par.(b)) <- par.(a) in

    let (@) y x = y * w + x in

    for y = 0 to h - 1 do
        for x = 0 to w - 1 do
            if s.(y).[x] = '#' then (
                if x > 0     && s.(y).[x - 1] = '.' then merge (y @ (x - 1)) (y @ x);
                if x < w - 1 && s.(y).[x + 1] = '.' then merge (y @ (x + 1)) (y @ x);
                if y > 0     && s.(y - 1).[x] = '.' then merge ((y - 1) @ x) (y @ x);
                if y < h - 1 && s.(y + 1).[x] = '.' then merge ((y + 1) @ x) (y @ x);
            )
        done
    done;
    let bl = Array.make hw 0 in
    let wh = Array.make hw 0 in
    for i = 0 to hw - 1 do
        let j = root i in
        let y = i / w in
        let x = i mod w in
        if s.(y).[x] = '#' then bl.(j) <- bl.(j) + 1
                           else wh.(j) <- wh.(j) + 1
    done;
    let rec loop i acc =
        if i = hw then acc else
            let acc = if bl.(i) >= 1 && wh.(i) >= 1 then acc + bl.(i) * wh.(i) else acc in
            loop (i + 1) acc
    in
    loop 0 0 |> Printf.printf "%d\n"
)