Problem Statement
Counting Rooms
Implementation
ll dx[4] = {1, -1, 0, 0};
ll dy[4] = {0, 0, 1, -1};
void solve()
{
ll n, m;
cin >> n >> m;
vector<string> s(n);
rep(i, n) cin >> s[i];
vvll vis(n + 1, vll(m + 1, 0));
function<void(ll, ll)> dfs = [&](ll x, ll y)
{
vis[x][y] = 1;
rep(k, 4)
{
ll i = x + dx[k], j = y + dy[k];
if (i >= 0 && i < n && j >= 0 && j < m && s[i][j] == '.' && !vis[i][j])
dfs(i, j);
}
};
ll cnt = 0LL;
rep(i, n)
{
rep(j, m)
{
if (s[i][j] == '.' && !vis[i][j])
{
dfs(i, j);
cnt++;
}
}
}
cout << cnt << "\n";
}