CSES - Shared codeLink to this code:
https://cses.fi/paste/6134996c08fd16a770e029/
#include <bits/stdc++.h>
#define uwu return 0;
using namespace std;
#define fs first
#define sc second
const int SIZE = 1e3 + 5, dx[4] = {1, 0, -1, 0}, dy[4] = {0, -1, 0, 1};
int n, m;
char cmap[SIZE][SIZE];
bool bmap[SIZE][SIZE];
bool able(int x,int y){
return (x > -1 && y > -1 && x < n && y < m && cmap[x][y] == '.' && (!bmap[x][y]));
}
void BFS(int x,int y){
queue<pair<int, int>> BFS;
BFS.push({x, y});
while(!BFS.empty()){
pair<int, int> temp = BFS.front();
BFS.pop();
for (int i = 0; i < 4;i++){
if(able(temp.fs + dx[i], temp.sc + dy[i])){
bmap[temp.fs + dx[i]][temp.sc + dy[i]] = 1;
BFS.push({temp.fs + dx[i], temp.sc + dy[i]});
}
}
}
return;
}
int main(){
cin >> n >> m;
for (int i = 0; i < n;i++){
for (int j = 0; j < m;j++){
cin >> cmap[i][j];
}
}
int answer = 0;
for (int i = 0; i < n;i++){
for (int j = 0; j < m;j++){
if(able(i,j)){
BFS(i, j);
answer++;
}
}
}
cout << answer << '\n';
uwu
}