Problem:
Score : 300 points

Problem Statement

There is a grid with H rows and W columns, where each square is painted black or white.

You are given H strings S_1, S_2, ..., S_H, each of length W.
If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is #; if that square is painted white, the j-th character in the string S_i is ..

Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:

There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...

Constraints

1 \leq H, W \leq 400

|S_i| = W (1 \leq i \leq H)

For each i (1 \leq i \leq H), the string S_i consists of characters # and ..

Input

Input is given from Standard Input in the following format:

H W
S_1
S_2
:
S_H

Output

Print the answer.

Sample Input 1

3 3
.#.
..#
#..

Sample Output 1

10

Some of the pairs satisfying the condition are ((1, 2), (3, 3)) and ((3, 1), (3, 2)), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.

Sample Input 2

2 4
....
....

Sample Output 2

0

Sample Input 3

4 3
###
###
...
###

Sample Output 3

6

Solution in C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

/**
 * File              : C - Alternating Path.cpp
 * Author            : horcrux2301 <khajuriaharsh729@gmail.com>
 * Date              : 12.01.2019 17:40:30
 * Last Modified Date: 12.01.2019 19:20:58
 */
#include <bits/stdc++.h>
#define ll long long
#define mp(a, b) make_pair(a, b)
#define newline cout << endl;
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

template <typename S, typename T> ostream& operator<<(ostream& out, pair<S, T> const& p)
{
    out << '(' << p.ff << ", " << p.ss << ')';
    return out;
}

template <typename T> ostream& operator<<(ostream& out, vector<T> const& v)
{
    int l = v.size();
    for (int i = 0; i < l - 1; i++)
        out << v[i] << ' ';
    if (l > 0)
        out << v[l - 1];
    return out;
}

template <typename T> void trace(const char* name, T&& arg1) { cout << name << " : " << arg1 << endl; }

template <typename T, typename... Args> void trace(const char* names, T&& arg1, Args&&... args)
{
    const char* comma = strchr(names + 1, ',');
    cout.write(names, comma - names) << " : " << arg1 << " | ";
    trace(comma + 1, args...);
}

bool validxy(ll x, ll y, ll a, ll b) { return x >= 0 && y >= 0 && x < a && y < b; }

struct HASH {
    size_t operator()(const pair<ll, ll>& x) const { return hash<long long>()(((ll)x.first) ^ (((long long)x.second) << 32)); }
};

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    clock_t tStart = clock();
    int test;
    test = 1;
    // cin >> test;
    while (test--) {
        ll a, b;
        cin >> a >> b;
        vector<string> arr(a);
        for (ll i = 0; i < a; i++)
            cin >> arr[i];

        ll ans = 0;

        bool visitedsharp[a][b];
        for (ll i = 0; i < a; i++) {
            for (ll j = 0; j < b; j++)
                visitedsharp[i][j] = 0;
        }

        for (ll ix = 0; ix < a; ix++) {
            for (ll jx = 0; jx < b; jx++) {

                if (arr[ix][jx] == '#' && !visitedsharp[ix][jx]) {

                    visitedsharp[ix][jx] = 1;
                    /* unordered_map<pll, bool, HASH> m; */
                    /* m[{ ix, jx }] = 1; */
                    ll cnt = 0;
                    ll valcnt = 1;
                    queue<pll> q;
                    bool visited[a][b];
                    for (ll i = 0; i < a; i++) {
                        for (ll j = 0; j < b; j++) {
                            visited[i][j] = 0;
                        }
                    }

                    q.push({ ix, jx });
                    visited[ix][jx] = 1;
                    /* trace("mainx,mainy", ix, jx); */

                    while (!q.empty()) {

                        auto node = q.front();
                        q.pop();
                        ll x = node.ff, y = node.ss;

                        for (ll i = -1; i <= 1; i++) {
                            for (ll j = -1; j <= 1; j++) {
                                if (abs(i) == abs(j))
                                    continue;
                                if (validxy(x + i, y + j, a, b) && !visited[x + i][y + j]) {
                                    if (arr[x][y] == '#' && arr[x + i][y + j] == '.') {
                                        visited[x + i][y + j] = 1;
                                        cnt++;
                                        /* trace("foundx,foundy", x + i, y + j); */
                                        q.push({ x + i, y + j });
                                    } else if (arr[x][y] == '.' && arr[x + i][y + j] == '#') {
                                        q.push({ x + i, y + j });
                                        visited[x + i][y + j] = 1;
                                        /* m[{ x + i, y + j }] = 1; */
                                        visitedsharp[x + i][y + j] = 1;
                                        valcnt++;
                                    }
                                }
                            }
                        }
                    }

                    ans += valcnt * cnt;
                }
            }
        }
        cout << ans << endl;
    }
#ifdef LOCAL_DEFINE
    printf("Time taken: %.2fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);
#endif
    return 0;
}