Language: C++

#include <bits/stdc++.h>
using namespace std;

struct Point{
	int h,w;
	Point(){};
	Point(int h_,int w_){
		h=h_,w=w_;
	};
};

Point operator+(const Point& a,const Point& b){return Point(a.h+b.h,a.w+b.w);}

struct Board{

	int H,W;
	vector<string>s ;
	vector<Point>near;
	queue<Point> que;
	vector<vector<int>> dist;
	deque<bool> visited;

	Board(){};
	Board(int h_,int w_){
		H=h_,W=w_;
		s.resize(H);
		near={{-1,0},{1,0},{0,-1},{0,1}};
		init_dist();
		init_visited();
	};

	bool in_board(Point p){if(0<=p.h && p.h<H && 0<=p.w && p.w<W)return true;else return false;} 
	char get_char(Point p){return s[p.h][p.w];}
	vector<Point> get_near(Point p){vector<Point>ret;for(auto&& u:near)ret.push_back(p+u);return ret;}

	int  get_dist(Point p){return dist[p.h][p.w];}
	int  set_dist(Point p,int i){dist[p.h][p.w]=i;}
	void init_dist(){dist.resize(H,vector<int>(W,-1));}

	int  is_visited(Point p){return visited[p.h][p.w];}
	int  set_visited(Point p,bool i){visited[p.h][p.w]=i;}
	void init_visited(){visited.resize(H,deque<bool>(W,false));}


	void dfs(Point p){
		if(is_visited(p))return;
		set_visited(p);
		for(auto&& u:get_near(p)){
		if(in_board(u) && !is_visited(u) && get_char(p)!=getchar(q)){
				if(get_char(u)=='.')ans++;
				dfs(u);
		}
		}
	}

	int do_all_dfs(){
		int ans=0;
		for (int i;i<H;i++)for(int j=0;j<W;j++){
		Point p(i,j);
		if(get_char(p)=='#'){
		init_visited();
		dfs(p);
		}}
		return ans;
	}

	void bfs(){
		while(! que.empty()){
		Point p=que.front();p.pop();	
		for(auto&& u:get_near(p)){
		if(in_board(u) &&get_dist(u)==-1){
			set_dist(u,get_dist(p)+1);
			if(get_cahr(u)=='.')ans++;
			que.push(u);
		}}
		}}

	void do_all_bfs(){for (int i;i<H;i++)for(int j=0;j<W;j++){
		Point p(i,j);
		if(get_dist(p)==-1){
			que.push(p);bfs();
		}
	}
	}
	

};


int main(){
	int h,w;cin>>h>>w;
	Board bd(h,w);
	for(auto&& u:bd.s)cin>>u;
	for(auto&& u:bd.s)cout<<u<<endl;
	cout<<bd.do_all_dfs()<<endl;
}

Compiler output:
a.cc: In member function 'int Board::set_dist(Point, int)':
a.cc:37:55: warning: no return statement in function returning non-void [-Wreturn-type]
   37 |         int  set_dist(Point p,int i){dist[p.h][p.w]=i;}
      |                                                       ^
a.cc: In member function 'int Board::is_visited(Point)':
a.cc:40:53: error: invalid types '__gnu_cxx::__alloc_traits<std::allocator<bool>, bool>::value_type {aka bool}[int]' for array subscript
   40 |         int  is_visited(Point p){return visited[p.h][p.w];}
      |                                                     ^
a.cc: In member function 'int Board::set_visited(Point, bool)':
a.cc:41:54: error: invalid types '__gnu_cxx::__alloc_traits<std::allocator<bool>, bool>::value_type {aka bool}[int]' for array subscript
   41 |         int  set_visited(Point p,bool i){visited[p.h][p.w]=i;}
      |                                                      ^
a.cc:41:62: warning: no return statement in function returning non-void [-Wreturn-type]
   41 |         int  set_visited(Point p,bool i){visited[p.h][p.w]=i;}
      |                                                              ^
a.cc: In member function 'void Board::init_visited()':
a.cc:42:43: error: no matching function for call to 'std::deque<bool>::resize(int&, std::deque<bool>)'
   42 |         void init_visited(){visited.resize(H,deque<bool>(W,false));}
      |                             ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/deque:66,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:139,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_deque.h:1288:7: note: candidate: 'void std::deque<_Tp, _Alloc>::resize(size_type) [with _Tp = bool; _Alloc = std::allocator<bool>; size_type = long unsigned int]'
 1288 |       resize(size_type __new_size)
      |       ^~~~~~
/usr/include/c++/14/bits/stl_deque.h:1288:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_deque.h:1310:7: note: candidate: 'void std::deque<_Tp, _Alloc>::resize(size_type, const value_type&) [with _Tp = bool; _Alloc = std::allocator<bool>; size_type = long unsigned int; value_type = bool]'
 1310 |       resize(size_type __new_size, const value_type& __x)
      |       ^~~~~~
/usr/include/c++/14/bits/stl_deque.h:1310:54: note:   no known conversion for argument 2 from 'std::deque<bool>' to 'const std::deque<bool>::value_type&' {aka 'const bool&'}
 1310 |       resize(size_type __new_size, const value_type& __x)
      |                                    ~~~~~~~~~~~~~~~~~~^~~
a.cc: In member function 'void Board::dfs(Point)':
a.cc:47:28: error: no matching function for call to 'Board::set_visited(Point&)'
   47 |                 set_visited(p);
      |                 ~~~~~~~~~~~^~~
a.cc:41:14: note: candidate: 'int Board::set_visited(Point, bool)'
   41 |         int  set_visited(Point p,bool i){visited[p.h][p.w]=i;}
      |              ^~~~~~~~~~~
a.cc:41:14: note:   candidate expects 2 arguments, 1 provided
a.cc:49:74: error: 'q' was not declared in this scope
   49 |                 if(in_board(u) && !is_visited(u) && get_char(p)!=getchar(q)){
      |                                                                          ^
a.cc:50:53: error: 'ans' was not declared in this scope; did you mean 'abs'?
   50 |                                 if(get_char(u)=='.')ans++;
      |                                                     ^~~
      |                                                     abs
a.cc: In member function 'void Board::bfs()':
a.cc:69:39: error: 'struct Point' has no member named 'pop'
   69 |                 Point p=que.front();p.pop();
      |                                       ^~~
a.cc:73:28: error: 'get_cahr' was not declared in this scope; did you mean 'get_char'?
   73 |                         if(get_cahr(u)=='.')ans++;
      |                            ^~~~~~~~
      |                            get_char
a.cc:73:45: error: 'ans' was not declared in this scope; did you mean 'abs'?
   73 |                         if(get_cahr(u)=='.')ans++;
      |                                             ^~~
      |                                             abs