Language: C++

#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, _s, n) for(int i = _s, i##_len = (n); i < i##_len; ++i)
#define ALL(x) (x).begin(), (x).end()
using namespace std;

#define repi(itr, ds) for(auto itr = ds.begin(); itr != ds.end(); itr++)
// vector
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
  for(T &x : vec) is >> x;
  return is;
}
// pair
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}
// vector
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "{";
  for(int i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
  }
  os << "}\n   ";
  return os;
}
// map
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
  os << "{";
  repi(itr, map_var) {
    os << *itr;
    itr++;
    if(itr != map_var.end()) os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
// set
template <typename T>
ostream &operator<<(ostream &os, set<T> &set_var) {
  os << "{";
  repi(itr, set_var) {
    os << *itr;
    itr++;
    if(itr != set_var.end()) os << ", ";
    itr--;
  }
  os << "}";
  return os;
}

#define DUMPOUT cerr

void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&... tail) {
  DUMPOUT << head;
  if(sizeof...(Tail) > 0) {
    DUMPOUT << ", ";
  }
  dump_func(std::move(tail)...);
}
#ifdef DEBUG_
#define DEB
#define dump(...)                                                             \
  DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                             \
          << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \
          << "    ",                                                          \
      dump_func(__VA_ARGS__)
#else
#define DEB if(false)
#define dump(...)
#endif

typedef long long int ll;

using namespace std;
class UnionFind {
 public:
  vector<ll> par;
  vector<ll> siz;

  // Constructor
  UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
    for(ll i = 0; i < sz_; ++i) par[i] = i;
  }
  void init(ll sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1LL);
    for(ll i = 0; i < sz_; ++i) par[i] = i;
  }

  // Member Function
  // Find
  ll root(ll x) {
    while(par[x] != x) {
      x = par[x] = par[par[x]];
    }
    return x;
  }

  // Union(Unite, Merge)
  bool merge(ll x, ll y) {
    x = root(x);
    y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(ll x, ll y) {  // 連結判定
    return root(x) == root(y);
  }

  ll size(ll x) {  // 素集合のサイズ
    return siz[root(x)];
  }
};

int main() {
  int H, W;
  cin >> H >> W;
  auto board = vector<vector<char>>(H, vector<char>(W));
  auto d = vector<bool>(H * W);
  REP(i, H) {
    string S;
    cin >> S;
    board[i] = vector<char>(ALL(S));
    REP(j, W) {
      if(board[i][j] == '#') {
        d[i * W + j] = true;
      }
    }
  }

  UnionFind uf = UnionFind(H * W);

  REP(i, H) {
    REP(j, W) {
      if(i + 1 < H && board[i][j] != board[i + 1][j]) {
        uf.merge(W * i + j, W * (i + 1) + j);
      }
      if(j + 1 < W && board[i][j] != board[i][j + 1]) {
        uf.merge(W * i + j, W * i + j + 1);
      }
    }
  }
  auto black = vector<ll>(H * W);
  auto white = vector<ll>(H * W);
  REP(i, H * W) {
    if(d[i]) {
      black[uf.root[i]]++;
    } else {
      white[uf.root[i]]++;
    }
  }
  ll ans = 0;
  REP(i, H * W) { ans += black[i] * white[i]; }
  cout << ans << endl;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:159:20: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  159 |       black[uf.root[i]]++;
      |                    ^
a.cc:161:20: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
  161 |       white[uf.root[i]]++;
      |                    ^