UVA572 code:
#include<bits/stdc++.h>
using namespace std;
int i,n,m,j,ans = 0;
char mapa[1001][1001] = {0};
int xp[9] = {0,0,1,0,-1,1,1,-1,-1},yp[9] = {0,1,0,-1,0,1,-1,1,-1};
int dfs(int x,int y){
if(mapa[x][y] != '#'){
return 0;
}
mapa[x][y] = '*';
for(int o = 0;o<9;o++){
if(mapa[x+xp[o]][y+yp[o]]=='#'){
dfs(x+xp[o],y+yp[o]);
}
}
return 2;
}
void print(){
for(int p = 0;p<n;p++){
for(int l = 0;l<m;l++){
cout<<mapa[p][l];
}
cout<<endl;
}
cout<<ans<<endl;
}
int did(int n,int m){
for(i = 0;i<n;i++){
for(j = 0;j<m;j++){
cin>>mapa[i][j];
if(mapa[i][j]=='*')mapa[i][j] = '.';
else mapa[i][j] = '#';
}
}
ans = 0;
// print();
for(i = 0;i<n;i++){
for(j = 0;j<m;j++){
if(dfs(i,j)==2){
ans++;
}
}
}
cout<<ans<<endl;
}
int main(){
while(true){
int na,ma;
cin>>na>>ma;
if(na==0 and ma==0)break;
else did(na,ma);
}
// system ("pause");
return 0;
}