Language: C++

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
	ll h,w;
	cin>>h>>w;
	string s[h];
	for(ll i=0;i<h;i++)
	cin>>s[i];
	vector< unordered_set< pair< ll, ll >  > > dp1(w);
	vector< vector< unordered_set< pair< ll, ll > > > > dp(h,dp1),dp2(h,dp1);
	for(ll i=0;i<h;i++)
	{
		for(ll j=0;j<w;j++)
		{
			dp[i][j].clear();
			dp2[i][j].clear();
		}
	}
	if(s[0][0]=='#')
	dp2[0][0].insert(make_pair(0,0));
	else
	dp[0][0].insert(make_pair(0,0));
	for(ll i=0;i<h;i++)
	{
		for(ll j=0;j<w;j++)
		{
			if(i==0&&j==0)
			continue;
			if(i-1>=0)
			{
				if(s[i-1][j]=='#'&&s[i][j]=='.')
				{
					unordered_set< pair< ll, ll > > :: iterator it;
					it=dp[i-1][j].begin();
					while(it!=dp[i-1][j].end())
					{
						dp[i][j].insert(*it);
						it++;
					}
					it=dp2[i-1][j].begin();
					while(it!=dp2[i-1][j].end())
					{
						dp2[i][j].insert(*it);
						it++;
					}
					dp[i][j].insert(make_pair(i,j));
				}
				else if(s[i-1][j]=='.'&&s[i][j]=='#')
				{
					unordered_set< pair< ll, ll > > :: iterator it;
					it=dp[i-1][j].begin();
					while(it!=dp[i-1][j].end())
					{
						dp[i][j].insert(*it);
						it++;
					}
					it=dp2[i-1][j].begin();
					while(it!=dp2[i-1][j].end())
					{
						dp2[i][j].insert(*it);
						it++;
					}
					dp2[i][j].insert(make_pair(i,j));
				}
			}
			if(j-1>=0)
			{
				if(s[i][j-1]=='#'&&s[i][j]=='.')
				{
					unordered_set< pair< ll, ll > > :: iterator it;
					it=dp2[i][j-1].begin();
					while(it!=dp2[i][j-1].end())
					{
						dp2[i][j].insert(*it);
						it++;
					}
					it=dp[i][j-1].begin();
					while(it!=dp[i][j-1].end())
					{
						dp[i][j].insert(*it);
						it++;
					}
					dp[i][j].insert(make_pair(i,j));
				}
				else if(s[i][j-1]=='.'&&s[i][j]=='#')
				{
					unordered_set< pair< ll, ll > > :: iterator it;
					it=dp[i][j-1].begin();
					while(it!=dp[i][j-1].end())
					{
						dp[i][j].insert(*it);
						it++;
					}
					it=dp2[i][j-1].begin();
					while(it!=dp2[i][j-1].end())
					{
						dp2[i][j].insert(*it);
						it++;
					}
					dp2[i][j].insert(make_pair(i,j));
				}
			}
			if(s[i][j]=='#')
				dp2[i][j].insert(make_pair(i,j));
				 if(s[i][j]=='.')
				dp[i][j].insert(make_pair(i,j));
		}
	}
	ll ans=0;
	for(ll i=0;i<h;i++)
	{
		for(ll j=0;j<w;j++)
		{
			if(s[i][j]=='#')
			ans+=dp[i][j].size();
			else
			ans+=dp2[i][j].size();
		}
	}
	cout<<ans<<endl;
	return 0;
}

Compiler output:
In file included from /usr/include/c++/14/bits/stl_tempbuf.h:61,
                 from /usr/include/c++/14/bits/stl_algo.h:69,
                 from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_construct.h: In instantiation of 'void std::_Construct(_Tp*, _Args&& ...) [with _Tp = unordered_set<pair<long long int, long long int> >; _Args = {}]':
/usr/include/c++/14/bits/stl_uninitialized.h:643:18:   required from 'static _ForwardIterator std::__uninitialized_default_n_1<_TrivialValueType>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = std::unordered_set<std::pair<long long int, long long int> >*; _Size = long unsigned int; bool _TrivialValueType = false]'
  643 |                 std::_Construct(std::__addressof(*__cur));
      |                 ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_uninitialized.h:712:20:   required from '_ForwardIterator std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = unordered_set<pair<long long int, long long int> >*; _Size = long unsigned int]'
  710 |       return __uninitialized_default_n_1<__is_trivial(_ValueType)
      |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  711 |                                          && __can_fill>::
      |                                          ~~~~~~~~~~~~~~~~
  712 |         __uninit_default_n(__first, __n);
      |         ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_uninitialized.h:779:44:   required from '_ForwardIterator std::__uninitialized_default_n_a(_ForwardIterator, _Size, allocator<_Tp>&) [with _ForwardIterator = unordered_set<pair<long long int, long long int> >*; _Size = long unsigned int; _Tp = unordered_set<pair<long long int, long long int> >]'
  779 |     { return std::__uninitialized_default_n(__first, __n); }
      |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1720:36:   required from 'void std::vector<_Tp, _Alloc>::_M_default_initialize(size_type) [with _Tp = std::unordered_set<std::pair<long long int, long long int> >; _Alloc = std::allocator<std::unordered_set<std::pair<long long int, long long int> > >; size_type = long unsigned int]'
 1720 |           std::__uninitialized_default_n_a(this->_M_impl._M_start, __n,
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1721 |                                            _M_get_Tp_allocator());
      |                                            ~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:558:9:   required from 'std::vector<_Tp, _Alloc>::vector(size_type, const allocator_type&) [with _Tp = std::unordered_set<std::pair<long long int, long long int> >; _Alloc = std::allocator<std::unordered_set<std::pair<long long int, long long int> > >; size_type = long unsigned int; allocator_type = std::allocator<std::unordered_set<std::pair<long long int, long long int> > >]'
  558 |       { _M_default_initialize(__n); }
      |         ^~~~~~~~~~~~~~~~~~~~~
a.cc:11:50:   required from here
   11 |         vector< unordered_set< pair< ll, ll >  > > dp1(w);
      |                                                         ^
/usr/include/c++/14/bits/stl_construct.h:119:7: error: use of deleted function 'std::unordered_set<_Value, _Hash, _Pred, _Alloc>::unordered_set() [with _Value = std::pair<long long int, long long int>; _Hash = std::hash<std::pair<long long int, long long int> >; _Pred = std::equal_to<std::pair<long long int, long long int> >; _Alloc = std::allocator<std::pair<long long int, long long int> >]'
  119 |       ::new((void*)__p) _Tp(std::forward<_Args>(__args)...);
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/unordered_set:41,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:189:
/usr/include/c++/14/bits/unordered_set.h:142:7: note: 'std::unordered_set<_Value, _Hash, _Pred, _Alloc>::unordered_set() [with _Value = std::pair<long long int, long long int>; _Hash = std::hash<std::pair<long long int, long long int> >; _Pred = std::equal_to<std::pair<long long int, long long int> >; _Alloc = std::allocator<std::pair<long long int, long long int> >]' is implicitly deleted because the default definition would be ill-formed:
  142 |       unordered_set() = default;
      |       ^~~~~~~~~~~~~
/usr/include/c++/14/bits/unordered_set.h:142:7: error: use of deleted function 'std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::_Hashtable() [with _Key = std::pair<long long int, long long int>; _Value = std::pair<long long int, long long int>; _Alloc = std::allocator<std::pair<long long int, long long int> >; _ExtractKey = std::__detail::_Identity; _Equal = std::equal_to<std::pair<long long int, long long int> >; _Hash = std::hash<std::pair<long long int, long long int> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, true, true>]'
In file included from /usr/include/c++/14/bits/unordered_map.h:33,
                 from /usr/include/c++/14/unordered_map:41,
                 from /usr/include/c++/14/functional:63,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53:
/usr/include/c++/14/bits/hashtable.h:539:7: note: 'std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::_Hashtable() [with _Key = std::pair<long long int, long long int>; _Value = std::pair<long long int, long long int>; _Alloc = std::allocator<std::pair<long long int, long long int> >; _ExtractKey = std::__detail::_Identity; _Equal = std::equal_to<std::pair<long long int, long long int> >; _Hash = std::hash<std::pair<long long int, long long int> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, true, true>]' is implicitly deleted because the default definition would be ill-formed:
  539 |       _Hashtable() = default;
      |       ^~~~~~~~~~
/usr/include/c++/14/bits/hashtable.h:539:7: error: use of deleted function 'std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _Traits>::_Hashtable_base() [with _Key = std::pair<long long int, long long int>; _Value = std::pair<long long int, long long int>; _ExtractKey = std::__detail::_Identity; _Equal = std::equal_to<std::pair<long long int, long long int> >; _Hash = std::hash<std::pair<long long int, long long int> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _Traits = std::__detail::_Hashtable_traits<true, true, true>]'
In file included from /usr/include/c++/14/bits/hashtable.h:35:
/usr/include/c++/14/bits/hashtable_policy.h:1731:7: note: 'std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _Traits>::_Hashtable_base() [with _Key = std::pair<long long int, long long int>; _Value = std::pair<long long int, long long int>; _ExtractKey = std::__detail::_Identity; _Equal = std::equal_to<std::pair<long long int, long long int> >; _Hash = std::hash<std::pair<long long int, long long int> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _Traits = std::__detail::_Hashtable_traits<true, true, true>]' is implicitly deleted because the default definition would be ill-formed:
 1731 |       _Hashtable_base() = default;
      |       ^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/hashtable_policy.h:1731:7: error: use of deleted function 'std::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Hash, _RangeHash, _Unused, __cache_hash_code>::_Hash_code_base() [with _Key = std::pair<long long int, long long int>; _Value = std::pair<long long int, long long int>; _ExtractKey = std::__detail::_Identity; _Hash = std::hash<std::pair<long long int, long long int> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; bool __cache_hash_code = true]'
/usr/include/c++/14/bits/hashtable_policy.h:1328:7: note: 'std::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Hash, _RangeHash, _Unused, __cache_hash_code>::_Hash_code_base() [with _Key = std::pair<long long int, long long int>; _Value = std::pair<long long int, long long int>; _ExtractKey = std::__detail::_Identity; _Hash = std::hash<std::pair<long long int, long long int> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; bool __cache_hash_code = true]' is implicitly deleted because the default definition would be ill-formed:
 1328 |       _Hash_code_base() = default;
      |       ^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/hashtable_policy.h:1328:7: error: use of deleted function 'std::__detail::_Hashtable_ebo_helper<1, std::hash<std::pair<long long int, long long int> >, true>::~_Hashtable_ebo_helper()'
/usr/include/c++/14/bits/hashtable_policy.h:1242:12: note: 'std::__detail::_Hashtable_ebo_helper<1, std::hash<std::pair<long long int, long long int> >, true>::~_Hashtable_ebo_helper()' is implicitly deleted because the default definition would be ill-formed:
 1242 |     struct _Hashtable_ebo_helper<_Nm, _Tp, true>
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/hashtable_policy.h:1242:12: error: use of deleted function 'std::hash<std::pair<long long int, long long int> >::~hash()'
In file included from /usr/include/c++/14/string_view:50,
                 from /usr/include/c++/14/bits/basic_string.h:47,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52:
/usr/include/c++/14/bits/functional_hash.h:102:12: note: 'std::hash<std::pair<long long int, long long int> >::~hash()' is implicitly deleted because the defaul