O1446 【入门】有多少细胞

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
char a[105][105];
int dx[] = {0, 0, 0, 1, -1};
int dy[] = {0, 1, -1, 0, 0};
bool check(int x,int y){
    if(x<1||x>n)return 0;
    if(y<1||y>m)return 0;
    if(a[x][y]=='.')return 0;
    return 1;
}
void bfs(int sx,int sy){
    queue<pair<int,int> >q;
    q.push({sx,sy});
    while(!q.empty()){
    	pair<int ,int>t=q.front();
    	int x=t.first,y=t.second;
    	q.pop();
    	for(int i = 1;i <= 4;i++){
    		int X=dx[i]+x,Y=dy[i]+y;
    		if(check(X,Y)){
    			a[X][Y] = '.';
    			q.push({X,Y});
			}
		}
	}
}
signed main(){
    cin >> n>>m;
    for(int i = 1; i <= n; i++){
        for (int j = 1;j<= m; j++){
            char z;
            cin >> z;
            if(z=='0')a[i][j]='.';
            else a[i][j]='W';
        }
    }
    int cnt=0;
    for(int i = 1; i<=n;i++){
        for (int j = 1;j<= m; j++){
            if(a[i][j] == 'W'){
            	cnt++;
//            	cout << i<<" "<<j<<endl;
            	bfs(i,j);
			}
        }
    }
    cout << cnt;
    return 0;
}