代码:
#include <iostream>
#include <queue>
#include <deque>
using namespace std;
struct point{
int x,y;
};
queue <point> q;
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
int map[105][105];
bool vis[105][105];
int n,m,sx,sy,tx,ty,color;
bool cj(int x,int y){
if (x>=0&&x<n&&y>=0&&y<m){
return false;
}else return true;
}
int bfs(int x,int y){
point tmp;
tmp.x=x;
tmp.y=y;
map[tmp.x][tmp.y]=0;
q.push(tmp);
while (!q.empty()){
tmp=q.front();
for (int i = 0;i<4;i++){
point np=tmp;
np.x+=dx[i];
np.y+=dy[i];
if (map[np.x][np.y]!=0&&cj(np.x,np.y)==false){
q.push(np);
map[np.x][np.y]=0;
}else continue;
}
q.pop();
}
}
int main(){
cin>>n>>m;
for (int i = 0;i<n;i++){
for (int j = 0;j<m;j++){
scanf("%1d",&map[i][j]);
}
}
for(int i = 0;i<n;i++){
for (int j = 0;j<m;j++){
if(map[i][j]!=0){
color++;
bfs(i,j);
}
}
}
cout<<color<<endl;
/*for (int i = 0;i<n;i++){
for (int j = 0;j<m;j++){
cout<<vis[i][j];
}
cout<<endl;
}*/
return 0;
}