1 #include<iostream> 2 #include<queue> 3 using namespace std; 4 char a[105][105]; 5 struct node{ 6 int x; 7 int y; 8 node(int x,int y) 9 { 10 this->x=x; 11 this->y=y; 12 } 13 }; 14 int dir[][2]={{0,1},{1,0},{0,-1},{-1,0}} ; 15 int vis[105][105]; 16 bool bush(int x,int y) 17 { 18 if(vis[x][y]==0&&a[x][y]=='#') 19 return true; 20 else 21 return false; 22 } 23 int main() 24 { 25 int n,m; 26 cin>>n>>m; 27 int count=0; 28 for(int i=1;i<=n;++i) 29 for(int j=1;j<=m;++j) 30 { 31 cin>>a[i][j]; 32 } 33 for(int i=1;i<=n;++i) 34 for(int j=1;j<=m;++j) 35 { 36 if(bush(i,j)) 37 { 38 queue<node> q; 39 node start = node(i,j); 40 q.push(start); 41 vis[i][j]=1; 42 while(!q.empty()) 43 { 44 node now = q.front(); 45 int x=now.x; 46 int y=now.y; 47 for(int k=0;k<4;k++) 48 { 49 if((x+dir[k][0])>0&&(x+dir[k][0]<=n)&&(y+dir[k][1]>0)&&(y+dir[k][1]<=m)) 50 { 51 if(bush(x+dir[k][0],y+dir[k][1])) 52 { 53 node next = node(x+dir[k][0],y+dir[k][1]); 54 q.push(next); 55 vis[x+dir[k][0]][y+dir[k][1]]=1; 56 } 57 } 58 } 59 q.pop(); 60 } 61 count++; 62 } 63 } 64 cout<<count; 65 return 0; 66 }