Language: C++

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<int>;using vvi=vector<vi>;using vl=vector<ll>;
using P=pair<int,int>;using PL=pair<ll,ll>;using vp=vector<P>;
const int INF=1<<30,MOD=1000000007;const ll LINF=1ll<<60;
#define _ol3(_1,_2,_3,name,...) name
#define _rep(i,n) _repi(i,0,n)
#define _repi(i,a,b) for(int i=a,i##_l=(b);i<i##_l;++i)
#define REP(...) _ol3(__VA_ARGS__,_repi,_rep,)(__VA_ARGS__)
#define REPR(i,n) for(int i=n-1;i>=0;--i)
#define REPA(i,v) REP(i,v.size())
#define all(v) (v).begin(),(v).end()
#define bit(n) (1ll<<(n))
#define clamp(n,mi,ma) max(min(n,ma),mi)
#define uniq(v) sort(all(v));v.erase(unique(all(v)),v.end())
#define pb push_back#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<int>;using vvi=vector<vi>;using vl=vector<ll>;
using P=pair<int,int>;using PL=pair<ll,ll>;using vp=vector<P>;
const int INF=1<<30,MOD=1000000007;const ll LINF=1ll<<60;
#define _ol3(_1,_2,_3,name,...) name
#define _rep(i,n) _repi(i,0,n)
#define _repi(i,a,b) for(int i=a,i##_l=(b);i<i##_l;++i)
#define REP(...) _ol3(__VA_ARGS__,_repi,_rep,)(__VA_ARGS__)
#define REPR(i,n) for(int i=n-1;i>=0;--i)
#define REPA(i,v) REP(i,v.size())
#define all(v) (v).begin(),(v).end()
#define bit(n) (1ll<<(n))
#define clamp(n,mi,ma) max(min(n,ma),mi)
#define uniq(v) sort(all(v));v.erase(unique(all(v)),v.end())
#define pb push_back
#define F first
#define S second
#define endl '\n'
#define cho(n,a,b) cout<<((n)?a:b)<<endl
#define YES(n) cho(n,"YES","NO")
#define Yes(n) cho(n,"Yes","No")
#define Poss(n) cho(n,"Possible","Impossible")
template<class T>bool chmax(T&a,const T&b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T&a,const T&b){if(a>b){a=b;return 1;}return 0;}
template<class T>ostream&operator<<(ostream&o,const vector<T>&v){for(auto&&i:v)o<<i<<' ';return o;}
template<class T>istream&operator>>(istream&i,vector<T>&v){for(T&j:v)i>>j;return i;}
template<class T,class U>ostream&operator<<(ostream&o,const pair<T,U>&p){return o<<p.F<<' '<<p.S;}
template<class T,class U>istream&operator>>(istream&i,pair<T,U>&p){return i>>p.F>>p.S;}
template<class T>ostream&operator<<(ostream&o,const set<T>&v){for(auto&&i:v)o<<i<<' ';return o;}
template<class T>ostream&operator<(ostream&o,const T&t){return o<<t;}
template<class T>istream&operator>(istream&i,T&t){return i>>t;}
#ifndef LOCAL
struct ostdmy{
template<class T>ostdmy&operator<<(const T&t){return*this;}
template<class T>ostdmy&operator<(const T&t){return*this;}};ostdmy cer_;
#define cerr cer_
#endif

vector<vp> uf;

P& operator+=(P&a, const P&b){
    a.F+=b.F;
    a.S+=b.S;
    return a;
}

void unite(int ax,int ay,int bx,int by){
    if(ax==bx && ay==by)return;
    for(auto a:uf){
        for(auto b:a){
            cerr<<b<<'\t';
        }cerr<<endl;
    }cerr<<endl;
    P& A=uf[ay][ax];
    P& B=uf[by][bx];
    if(A.F>=0 && A.S>=0){
        return unite(A.S,A.F,bx,by);
    }
    if(B.F>=0 && B.S>=0){
        return unite(ax,ay,B.S,B.F);
    }
    A+=B;
    B={ay,ax};
}

int main(){
    cin.tie(0);ios::sync_with_stdio(0);
    int h,w;
    cin>h>w;
    uf = vector<vp>(h,vp(w));

    vector<string> s(h);
    cin>s;
    REP(y,h){
        REP(x,w){
            if(s[y][x]=='#')
                uf[y][x]={-1,0};
            else
                uf[y][x]={0,-1};
        }
    }
    REP(y,h){
        REP(x,1,w){
            if(s[y][x]!=s[y][x-1]){
                unite(x,y,x-1,y);
            }
        }
    }
    REP(y,1,h){
        REP(x,w){
            if(s[y][x]!=s[y-1][x]){
                unite(x,y,x,y-1);
            }
        }
    }
    int ans=0;
    REP(y,h){
        REP(x,w){
            auto&& n=uf[y][x];
            if(n.F<0 || n.S<0){
                ans+=n.F*n.S;
            }
        }
    }
    cout<ans<endl;
    return 0;
}

#define F first
#define S second
#define endl '\n'
#define cho(n,a,b) cout<<((n)?a:b)<<endl
#define YES(n) cho(n,"YES","NO")
#define Yes(n) cho(n,"Yes","No")
#define Poss(n) cho(n,"Possible","Impossible")
template<class T>bool chmax(T&a,const T&b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T&a,const T&b){if(a>b){a=b;return 1;}return 0;}
template<class T>ostream&operator<<(ostream&o,const vector<T>&v){for(auto&&i:v)o<<i<<' ';return o;}
template<class T>istream&operator>>(istream&i,vector<T>&v){for(T&j:v)i>>j;return i;}
template<class T,class U>ostream&operator<<(ostream&o,const pair<T,U>&p){return o<<p.F<<' '<<p.S;}
template<class T,class U>istream&operator>>(istream&i,pair<T,U>&p){return i>>p.F>>p.S;}
template<class T>ostream&operator<<(ostream&o,const set<T>&v){for(auto&&i:v)o<<i<<' ';return o;}
template<class T>ostream&operator<(ostream&o,const T&t){return o<<t;}
template<class T>istream&operator>(istream&i,T&t){return i>>t;}
#ifndef LOCAL
struct ostdmy{
template<class T>ostdmy&operator<<(const T&t){return*this;}
template<class T>ostdmy&operator<(const T&t){return*this;}};ostdmy cer_;
#define cerr cer_
#endif

class UnionFind{
private:
	vi data;
public:
	UnionFind(int size):data(size,-1){}
	bool unite(int x,int y){
		x=root(x);y=root(y);
		if(x!=y){
			if(data[y]<data[x])swap(x,y);
			data[x]+=data[y];data[y]=x;
		}
		return x!=y;
	}
	bool find(int x,int y){
		return root(x)==root(y);
	}
	int root(int x){
		return data[x]<0?x:data[x]=root(data[x]);
	}
	int size(int x){
		return -data[root(x)];
	}
};

vector<vp> uf;

P& operator+=(P&a, const P&b){
    a.F+=b.F;
    a.S+=b.S;
    return a;
}

void unite(int ax,int ay,int bx,int by){
    if(ax==bx && ay==by)return;
    for(auto a:uf){
        for(auto b:a){
            cerr<<b<<'\t';
        }cerr<<endl;
    }cerr<<endl;
    P& A=uf[ay][ax];
    P& B=uf[by][bx];
    if(A.F>=0 && A.S>=0){
        return unite(A.S,A.F,bx,by);
    }
    if(B.F>=0 && B.S>=0){
        return unite(ax,ay,B.S,B.F);
    }
    A+=B;
    B={ay,ax};
}

int main(){
    cin.tie(0);ios::sync_with_stdio(0);
    int h,w;
    cin>h>w;
    uf = vector<vp>(h,vp(w));

    vector<string> s(h);
    cin>s;
    cerr<<"Yay"<<flush;
    REP(y,h){
        REP(x,w){
            if(s[y][x]=='#')
                uf[y][x]={-1,0};
            else
                uf[y][x]={0,-1};
        }
    }
    cerr<<"Yay"<<flush;
    REP(y,h){
        REP(x,1,w){
            if(s[y][x]!=s[y][x-1]){
                unite(x,y,x-1,y);
            }
        }
    }
    cerr<<"Yay"<<flush;
    REP(y,1,h){
        REP(x,w){
            if(s[y][x]!=s[y-1][x]){
                unite(x,y,x,y-1);
            }
        }
    }
    cerr<<"Yay"<<flush;
    int ans=0;
    REP(y,h){
        REP(x,w){
            auto&& n=uf[y][x];
            if(n.F<0 || n.S<0){
                ans+=n.F*n.S;
            }
        }
    }
    cout<ans<endl;
    return 0;
}


Compiler output:
a.cc:33:9: warning: "pb" redefined
   33 | #define pb push_back
      |         ^~
a.cc:17:9: note: this is the location of the previous definition
   17 | #define pb push_back#include <bits/stdc++.h>
      |         ^~
a.cc:22:11: error: redefinition of 'const int INF'
   22 | const int INF=1<<30,MOD=1000000007;const ll LINF=1ll<<60;
      |           ^~~
a.cc:6:11: note: 'const int INF' previously defined here
    6 | const int INF=1<<30,MOD=1000000007;const ll LINF=1ll<<60;
      |           ^~~
a.cc:22:21: error: redefinition of 'const int MOD'
   22 | const int INF=1<<30,MOD=1000000007;const ll LINF=1ll<<60;
      |                     ^~~
a.cc:6:21: note: 'const int MOD' previously defined here
    6 | const int INF=1<<30,MOD=1000000007;const ll LINF=1ll<<60;
      |                     ^~~
a.cc:22:45: error: redefinition of 'const ll LINF'
   22 | const int INF=1<<30,MOD=1000000007;const ll LINF=1ll<<60;
      |                                             ^~~~
a.cc:6:45: note: 'const ll LINF' previously defined here
    6 | const int INF=1<<30,MOD=1000000007;const ll LINF=1ll<<60;
      |                                             ^~~~
a.cc:134:23: error: redefinition of 'template<class T> bool chmax(T&, const T&)'
  134 | template<class T>bool chmax(T&a,const T&b){if(a<b){a=b;return 1;}return 0;}
      |                       ^~~~~
a.cc:41:23: note: 'template<class T> bool chmax(T&, const T&)' previously declared here
   41 | template<class T>bool chmax(T&a,const T&b){if(a<b){a=b;return 1;}return 0;}
      |                       ^~~~~
a.cc:135:23: error: redefinition of 'template<class T> bool chmin(T&, const T&)'
  135 | template<class T>bool chmin(T&a,const T&b){if(a>b){a=b;return 1;}return 0;}
      |                       ^~~~~
a.cc:42:23: note: 'template<class T> bool chmin(T&, const T&)' previously declared here
   42 | template<class T>bool chmin(T&a,const T&b){if(a>b){a=b;return 1;}return 0;}
      |                       ^~~~~
a.cc:136:26: error: redefinition of 'template<class T> std::ostream& operator<<(std::ostream&, const std::vector<_Tp>&)'
  136 | template<class T>ostream&operator<<(ostream&o,const vector<T>&v){for(auto&&i:v)o<<i<<' ';return o;}
      |                          ^~~~~~~~
a.cc:43:26: note: 'template<class T> std::ostream& operator<<(std::ostream&, const std::vector<_Tp>&)' previously declared here
   43 | template<class T>ostream&operator<<(ostream&o,const vector<T>&v){for(auto&&i:v)o<<i<<' ';return o;}
      |                          ^~~~~~~~
a.cc:137:26: error: redefinition of 'template<class T> std::istream& operator>>(std::istream&, std::vector<_Tp>&)'
  137 | template<class T>istream&operator>>(istream&i,vector<T>&v){for(T&j:v)i>>j;return i;}
      |                          ^~~~~~~~
a.cc:44:26: note: 'template<class T> std::istream& operator>>(std::istream&, std::vector<_Tp>&)' previously declared here
   44 | template<class T>istream&operator>>(istream&i,vector<T>&v){for(T&j:v)i>>j;return i;}
      |                          ^~~~~~~~
a.cc:138:34: error: redefinition of 'template<class T, class U> std::ostream& operator<<(std::ostream&, const std::pair<_T1, _T2>&)'
  138 | template<class T,class U>ostream&operator<<(ostream&o,const pair<T,U>&p){return o<<p.F<<' '<<p.S;}
      |                                  ^~~~~~~~
a.cc:45:34: note: 'template<class T, class U> std::ostream& operator<<(std::ostream&, const std::pair<_T1, _T2>&)' previously declared here
   45 | template<class T,class U>ostream&operator<<(ostream&o,const pair<T,U>&p){return o<<p.F<<' '<<p.S;}
      |                                  ^~~~~~~~
a.cc:139:34: error: redefinition of 'template<class T, class U> std::istream& operator>>(std::istream&, std::pair<_T1, _T2>&)'
  139 | template<class T,class U>istream&operator>>(istream&i,pair<T,U>&p){return i>>p.F>>p.S;}
      |                                  ^~~~~~~~
a.cc:46:34: note: 'template<class T, class U> std::istream& operator>>(std::istream&, std::pair<_T1, _T2>&)' previously declared here
   46 | template<class T,class U>istream&operator>>(istream&i,pair<T,U>&p){return i>>p.F>>p.S;}
      |                                  ^~~~~~~~
a.cc:140:26: error: redefinition of 'template<class T> std::ostream& operator<<(std::ostream&, const std::set<T>&)'
  140 | template<class T>ostream&operator<<(ostream&o,const set<T>&v){for(auto&&i:v)o<<i<<' ';return o;}
      |                          ^~~~~~~~
a.cc:47:26: note: 'template<class T> std::ostream& operator<<(std::ostream&, const std::set<T>&)' previously declared here
   47 | template<class T>ostream&operator<<(ostream&o,const set<T>&v){for(auto&&i:v)o<<i<<' ';return o;}
      |                          ^~~~~~~~
a.cc:141:26: error: redefinition of 'template<class T> std::ostream& operator<(std::ostream&, const T&)'
  141 | template<class T>ostream&operator<(ostream&o,const T&t){return o<<t;}
      |                          ^~~~~~~~
a.cc:48:26: note: 'template<class T> std::ostream& operator<(std::ostream&, const T&)' previously declared here
   48 | template<class T>ostream&operator<(ostream&o,const T&t){return o<<t;}
      |                          ^~~~~~~~
a.cc:142:26: error: redefinition of 'template<class T> std::istream& operator>(std::istream&, T&)'
  142 | template<class T>istream&operator>(istream&i,T&t){return i>>t;}
      |                          ^~~~~~~~
a.cc:49:26: note: 'template<class T> std::istream& operator>(std::istream&, T&)' previously declared here
   49 | template<class T>istream&operator>(istream&i,T&t){return i>>t;}
      |                          ^~~~~~~~
a.cc:144:8: error: redefinition of 'struct ostdmy'
  144 | struct ostdmy{
      |        ^~~~~~
a.cc:51:8: note: previous definition of 'struct ostdmy'
   51 | struct ostdmy{
      |        ^~~~~~
a.cc:146:68: error: redefinition of 'ostdmy cer_'
  146 | template<class T>ostdmy&operator<(const T&t){return*this;}};ostdmy cer_;
      |                                                                    ^~~~
a.cc:53:68: note: 'ostdmy cer_' previously declared here
   53 | template<class T>ostdmy&operator<(const T&t){return*this;}};ostdmy cer_;
      |                                                                    ^~~~
a.cc:174:12: error: redefinition of 'std::vector<std::vector<std::pair<int, int> > > uf'
  174 | vector<vp> uf;
      |            ^~
a.cc:57:12: note: 'std::vector<std::vector<std::pair<int, int> > > uf' previously declared here
   57 | vector<vp> uf;
      |            ^~
a.cc:176:4: error: redefinition of 'P& operator+=(P&, const P&)'
  176 | P& operator+=(P&a, const P&b){
      |    ^~~~~~~~
a.cc:59:4: note: 'P& operator+=(P&, const P&)' previously defined here
   59 | P& operator+=(P&a, const P&b){
      |    ^~~~~~~~
a.cc:182:6: error: redefinition of 'void unite(int, int, int, int)'
  182 | void unite(int ax,int ay,int bx,int by){
      |      ^~~~~
a.cc:65:6: note: 'void unite(int, int, int, int)' previously defined here
   65 | void unite(int ax,int ay,int bx,int by){
      |      ^~~~~
a.cc:201:5: error: redefinition of 'int main()'
  201 | int main(){
      |     ^~~~
a.cc:84:5: note: 'int main()' previously defined here
   84 | int main(){
      |     ^~~~
a.cc: In function 'int main()':
a.cc:209:16: error: no match for 'operator<<' (operand types are 'ostdmy' and '<unresolved overloaded function type>')
  209 |     cerr<<"Yay"<<flush;
      |                ^
a.cc:52:25: note: candidate: 'template<class T> ostdmy& ostdmy::operator<<(const T&)'
   52 | template<class T>ostdmy&operator<<(const T&t){return*this;}
      |                         ^~~~~~~~
a.cc:52:25: note:   template argument deduction/substitution failed:
a.cc:209:18: note:   couldn't deduce template parameter 'T'
  209 |     cerr<<"Yay"<<flush;
      |                  ^~~~~
a.cc:43:26: note: candidate: 'template<class T> std::ostream& operator<<(std::ostream&, const std::vector<_Tp>&)'
   43 | template<class T>ostream&operator<<(ostream&o,const vector<T>&v){for(auto&&i:v)o<<i<<' ';return o;}
      |                          ^~~~~~~~
a.cc:43:26: note:   template argument deduction/substitution failed:
a.cc:209:18: note:   couldn't deduce template parameter 'T'
  209 |     cerr<<"Yay"<<flush;
      |                  ^~~~~
a.cc:45:34: note: candidate: 'template<class T, class U> std::ostream& operator<<(std::ostream&, const std::pair<_T1, _T2>&)'
   45 | template<class T,class U>ostream&operator<<(ostream&o,const pair<T,U>&p){return o<<p.F<<' '<<p.S;}
      |                                  ^~~~~~~~
a.cc:45:34: note:   template argument deduction/substitution failed:
a.cc:209:18: note:   couldn't deduce template parameter 'T'
  209 |     cerr<<"Yay"<<flush;
      |                  ^~~~~
a.cc:47:26: note: candidate: 'template<class T> std::ostream& operator<<(std::ostream&, const std::set<T>&)'
   47 | template<class T>ostream&operator<<(ostream&o,const set<T>&v){for(auto&&i:v)o<<i<<' ';return o;}
      |                          ^~~~~~~~
a.cc:47:26: note:   template argument deduction/substitution failed:
a.cc:209:18: note:   couldn't deduce template parameter 'T'
  209 |     cerr<<"Yay"<<flush;
      |                  ^~~~~
In file included from /usr/include/c++/14/regex:68,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:181,
                 from a.cc:1:
/usr/include/c++/14/bits/regex.h:1715:5: note: candidate: 'template<class _Ch_type, class _Ch_traits, class _Bi_iter> std::basic_ostream<_CharT, _Traits>& std::__cxx11::operator<<(std::basic_ostream<_CharT, _Traits>&, const sub_match<_Bi_iter>&)'
 1715 |     operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1715:5: note:   template argument deduction/substitution failed:
a.cc:209:18: note:   'ostdmy' is not derived from 'std::basic_ostream<_CharT, _Traits>'
  209 |     cerr<<"Yay"<<flush;
      |                  ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:41:
/usr/include/c++/14/cstddef:125:5: note: candidate: 'template<class _IntegerType> constexpr std::__byte_op_t<_IntegerType> std::operator<<(byte, _IntegerType)'
  125 |     operator<<(byte __b, _IntegerType __shift) noexcept
      |     ^~~