#!/usr/bin/env python
"""
This file is part of https://github.com/Cheran-Senthil/PyRival.

Copyright 2018 Cheran Senthilkumar all rights reserved,
Cheran Senthilkumar <hello@cheran.io>
Permission to use, modify, and distribute this software is given under the
terms of the MIT License.

"""
from __future__ import division, print_function

import cmath
import itertools
import math
import operator as op
# import random
import sys
from atexit import register
from bisect import bisect_left, bisect_right
# from collections import Counter, MutableSequence, defaultdict, deque
# from copy import deepcopy
# from decimal import Decimal
# from difflib import SequenceMatcher
# from fractions import Fraction
# from heapq import heappop, heappush

if sys.version_info[0] < 3:
    # from cPickle import dumps
    from io import BytesIO as stream
    # from Queue import PriorityQueue, Queue
else:
    # from functools import reduce
    from io import StringIO as stream
    # from pickle import dumps
    # from queue import PriorityQueue, Queue


if sys.version_info[0] < 3:
    class dict(dict):
        """dict() -> new empty dictionary"""
        def items(self):
            """D.items() -> a set-like object providing a view on D's items"""
            return dict.iteritems(self)

        def keys(self):
            """D.keys() -> a set-like object providing a view on D's keys"""
            return dict.iterkeys(self)

        def values(self):
            """D.values() -> an object providing a view on D's values"""
            return dict.itervalues(self)

    input = raw_input
    range = xrange

    filter = itertools.ifilter
    map = itertools.imap
    zip = itertools.izip


def sync_with_stdio(sync=True):
    """Set whether the standard Python streams are allowed to buffer their I/O.

    Args:
        sync (bool, optional): The new synchronization setting.

    """
    global input, flush

    if sync:
        flush = sys.stdout.flush
    else:
        sys.stdin = stream(sys.stdin.read())
        input = lambda: sys.stdin.readline().rstrip('\r\n')

        sys.stdout = stream()
        register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))


def gcd(x, y):
    """greatest common divisor of x and y"""
    while y:
        x, y = y, x % y
    return x


def connected_components(n, graph):
    components, visited = [], [False] * n

    def dfs(start):
        component, stack = [], [start]

        while stack:
            start = stack[-1]

            if visited[start]:
                stack.pop()
                continue
            else:
                visited[start] = True
                component.append(start)

            for i in graph[start]:
                if not visited[i]:
                    stack.append(i)

        return component

    for i in range(n):
        if not visited[i]:
            components.append(dfs(i))

    return components


def main():
    h, w = map(int, input().split(' '))
    s = [input() for _ in range(h)]

    black, white = set(), set()
    for i in range(h):
        for j in range(w):
            if s[i][j] == '.':
                black.add(i*w + j)
            else:
                white.add(i*w + j)

    edges = []

    for i in range(h):
        for j in range(w):
            if s[i][j] == '.':
                if i > 0:
                    if s[i - 1][j] == '#':
                        edges.append((i*w + j, (i - 1)*w + j))
                        edges.append(((i - 1)*w + j, i*w + j))
                if i < h - 1:
                    if s[i + 1][j] == '#':
                        edges.append((i*w + j, (i + 1)*w + j))
                        edges.append(((i + 1)*w + j, i*w + j))
                if j > 0:
                    if s[i][j - 1] == '#':
                        edges.append((i*w + j, i*w + (j - 1)))
                        edges.append((i*w + (j - 1), i*w + j))
                if j < w - 1:
                    if s[i][j + 1] == '#':
                        edges.append((i*w + j, i*w + (j + 1)))
                        edges.append((i*w + (j + 1), i*w + j))

    graph = [[] for _ in range(h*w)]
    for edge in edges:
        x, y = edge
        graph[x].append(y)

    res = 0
    for comp in connected_components(h*w, graph):
        cnt1, cnt2 = 0, 0
        for j in comp:
            if j in black:
                cnt1 += 1
            else:
                cnt2 += 1
        res += cnt1 * cnt2

    print(res)


if __name__ == '__main__':
    sync_with_stdio(False)
    main()


Measured memory_bytes: 101280