Language: C++

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <numeric> // accumulate(v.begin(), v.end(), 0)

using namespace std;
#define ll long long

#define MAX_N 100000

int par[MAX_N]; // 親の番号
int rank[MAX_N];

// n要素で初期化
void init(int n){
  for(int i=0; i<n; i++){
    par[i] = i;
    rank[i] = 0;
  }
}
// 木の根を求める
int root(int x){
  if(par[x] == x){ // 根
    return x;
  } else {
    return par[x] = root(par[x]); // 経路圧縮
  }
}

// xとyが同じ集合に属するか否か
bool same(int x, int y){
  return root(x) == root(y);
}

// xとyの属する集合を併合
void unite(int x, int y){
  x = root(x);
  y = root(y);
  if(x == y) return;

  if(rank[x] < rank[y]){
    par[x] = y;
  } else {
    par[y] = x;
    if(rank[x] == rank[y])
      rank[x]++;
  }
}

bool M[405][405];
void sub(int i1, int j1, int i2, int j2, int W) {
  if(M[i1][j1] xor M[i2][j2]) {
    unite(i1*W+j1, i2*W+j2);
  }
}

int main(){
  int H, W;
  cin >> H >> W;
  for(int i=0; i<H; i++) {
    string S;
    cin >> S;
    for(int j=0; j<W; j++) {
      if(S[j] == '.') // white
	M[i][j] = true;
      else // black
	M[i][j] = false;
    }
  }
  init(H*W);
  for(int i=0; i<H; i++) {
    for(int j=0; j<W; j++) {
      bool self = M[i][j];
      // if(i != 0)
      // 	sub(i, j, i-1, j, W);
      if(i != H-1)
	sub(i, j, i+1, j, W);
      // if(j != 0)
      // 	sub(i, j, i, j-1, W);
      if(j != W-1)
	sub(i, j, i, j+1, W);
    }
  }
  vector<int> white(H*W, 0);
  vector<int> black(H*W, 0);
  for(int i=0; i<H; i++) {
    for(int j=0; j<W; j++) {
      if(M[i][j]) { // white
	white[root(i*W+j)]++;
      } else { // black
	black[root(i*W+j)]++;
      }
    }
  }
  int ans = 0;
  for(int i=0; i<H*W; i++)
    ans += white[i] * black[i];
  cout << ans << endl;
  return 0;
}

Compiler output:
a.cc: In function 'void init(int)':
a.cc:20:5: error: reference to 'rank' is ambiguous
   20 |     rank[i] = 0;
      |     ^~~~
In file included from /usr/include/c++/14/bits/move.h:37,
                 from /usr/include/c++/14/bits/exception_ptr.h:41,
                 from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/ios:41,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:14:5: note:                 'int rank [100000]'
   14 | int rank[MAX_N];
      |     ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:43:6: error: reference to 'rank' is ambiguous
   43 |   if(rank[x] < rank[y]){
      |      ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:14:5: note:                 'int rank [100000]'
   14 | int rank[MAX_N];
      |     ^~~~
a.cc:43:16: error: reference to 'rank' is ambiguous
   43 |   if(rank[x] < rank[y]){
      |                ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:14:5: note:                 'int rank [100000]'
   14 | int rank[MAX_N];
      |     ^~~~
a.cc:47:8: error: reference to 'rank' is ambiguous
   47 |     if(rank[x] == rank[y])
      |        ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:14:5: note:                 'int rank [100000]'
   14 | int rank[MAX_N];
      |     ^~~~
a.cc:47:19: error: reference to 'rank' is ambiguous
   47 |     if(rank[x] == rank[y])
      |                   ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:14:5: note:                 'int rank [100000]'
   14 | int rank[MAX_N];
      |     ^~~~
a.cc:48:7: error: reference to 'rank' is ambiguous
   48 |       rank[x]++;
      |       ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:14:5: note:                 'int rank [100000]'
   14 | int rank[MAX_N];
      |     ^~~~