Link to this code:
https://cses.fi/paste/2350bdff16d44f972eaa96///This code is written by प्रविण शंखपाळ
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define flash ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD 1000000007
#define MOD1 998244353
#define INF 1e18
#define nline "\n"
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define srt(x) sort(x.begin(),x.end())
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
//typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
ll power(ll a, ll b){ll ans=1; while(b){if(b&1)ans = (ans*a)%MOD;a = (a*a)%MOD;b>>=1;} return ans;}
ll power1(ll a, ll b){ll ans=1; while(b){if(b&1)ans = (ans*a);a = (a*a);b>>=1;} return ans;}
ll dx[] = {1,0,-1,0,1,-1,1,-1};
ll dy[] = {0,1,0,-1,1,1,-1,-1};
string ds = "RLDU";
vector<vector<char>> A;
vector<vector<ll>> vis;
bool possible(ll x, ll y, ll n, ll m){
if(x >= 0 && y >= 0 && x < n && y < m){
return true;
}
return false;
}
void dfs(ll x, ll y, ll n, ll m){
// debug(vis)
if(possible(x, y, n, m) == false || vis[x][y] == 1 || A[x][y] == '#')return;
vis[x][y] = 1;
for(ll i = 0; i < 4 ; i ++){
// vis[x+dx[i]][y+dy[i]] = 1;
dfs(x+dx[i],y+dy[i], n, m);
// debug(vis)
}
}
void solve(){
// use brackets for binary operations.
// use .lower_bound()
// __lg for log
// Think about all data structures first and which will be easy
// to implement. vector, set, multiset, unordered_set, ordered_set(pbds),
// map, multimap, unordered_map(custom hashing), Queue, priority_queue, stack.
ll t=1;
//cin >> t;
while(t--){
ll n, m, cnt = 0;
cin >> n >> m;
A.resize(n,vector<char>(m));vis.resize(n,vector<ll>(m,0));
// vector<vector<char>> A(n,vector<char>(m));
// vector<vector<ll>> vis(n,vector<ll>(m,0));
for(ll i = 0 ; i < n ; i++){
for(ll j = 0; j < m ; j++){
cin >> A[i][j];
}
}
for(ll i = 0 ; i < n ; i++){
for(ll j = 0; j < m ; j++){
if(A[i][j]=='.'&&vis[i][j]==0){
dfs(i,j,n,m);
cnt++;
//debug(cnt)
}
}
}
cout << cnt << "\n";
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("Error.txt", "w", stderr);
#endif
flash
solve();
// cout<<fixed<<setprecision(10)<<mx<<"\n";
}