#include <cstddef>
#include <complex>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <array>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <limits>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <valarray>
#include <utility>
#include <tuple>
#include <cmath>
#include <memory>
#include <random>
#include <fstream>
#include <locale>

using ll = long long;
using ull = unsigned long long;

//vector
template <class T>
using vec = std::vector<T>;

template <class T>
using vvec = vec<vec<T>>;

constexpr std::size_t operator""_sz(ull n) { return std::size_t(n); }

constexpr char white = '.';
constexpr char black = '#';

int rec(const vvec<char> &board, vvec<int> &group, int y, int x, int h, int w, int g)
{
    if (y > h || y <= 0 || x > w || x <= 0 || group[y][x] != 0 || board[y][x] == ' ')
        return 0;

    group[y][x] = g;
    //std::cout << y << " " << x << std::endl;

    int ret{1};

    if (board[y][x] == white)
    {
        for (int i = -1; i <= 1; ++i)
            for (int j = -1; j <= 1; ++j)
            {
                if (abs(i) + abs(j) <= 1 && board[y + i][x + j] == black)
                    ret += rec(board, group, y + i, x + j, h, w, g);
            }
    }
    else if (board[y][x] == black)
    {
        for (int i = -1; i <= 1; ++i)
            for (int j = -1; j <= 1; ++j)
            {
                if (abs(i) + abs(j) <= 1 && board[y + i][x + j] == white)
                    ret += rec(board, group, y + i, x + j, h, w, g);
            }
    }

    return ret;
}

int main()
{
    using namespace std;

    int h, w;
    cin >> h >> w;
    vvec<char> board(h + 2, vec<char>(w + 2, 'a'));

    vec<pair<int, int>> blacks;

    for (int i = 1; i <= h; ++i)
        for (int j = 1; j <= w; ++j)
        {
            cin >> board[i][j];
            //board[i][j] = (i + j) % 2 == 0 ? black : white;
            if (board[i][j] == black)
                blacks.push_back(make_pair(i, j));
        }

    vvec<int> group(h + 2, vec<int>(w + 2));
    for (int i = 0; i <= h + 1; ++i)
    {
        group[i][0] = -1;
        group[i][w + 1] = -1;
    }
    for (int i = 0; i <= w + 1; ++i)
    {
        group[0][i] = -1;
        group[h + 1][i] = -1;
    }

    int g{1};

    for (int i = 1; i <= h; ++i)
        for (int j = 1; j <= w; ++j)
        {
            if (group[i][j] == 0)
            {
                auto r = rec(board, group, i, j, h, w, g);
                if (r > 0)
                {
                    ++g;
                }
            }
        }

    // black, white
    vec<pair<ll, ll>> gs(g);

    for (int i = 1; i <= h; ++i)
        for (int j = 1; j <= w; ++j)
        {
            auto index = group[i][j];
            if (board[i][j] == black)
            {
                ++gs[index].first;
            }
            else if (board[i][j] == white)
            {
                ++gs[index].second;
            }
        }

    ll ans {};
    for (auto p : gs)
    {
        ans += p.first * p.second;
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 19192