![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 #include<string.h> 5 #include<ctype.h> 6 7 char map[101][101]; 8 int visit[101][101],m,n; 9 int dx[] = {0,1,-1, 0,1, 1,-1,-1}; 10 int dy[] = {1,0, 0,-1,1,-1,-1, 1}; 11 12 void DFS(int x,int y) 13 { 14 int i, x1, y1; 15 visit[x][y] = 1; 16 for(i=0; i<8; i++) 17 { 18 x1 = x + dx[i]; 19 y1 = y + dy[i]; 20 if(!visit[x1][y1]&&map[x1][y1]=='@'&&x1>=0&&x1<m&&y1>=0&&y1<n) 21 { 22 visit[x1][y1] = 1; 23 DFS(x1,y1); 24 } 25 } 26 } 27 28 int main() 29 { 30 int i,j,count; 31 32 while(scanf("%d%d",&m,&n)&&m&&n) 33 { 34 memset(visit,0,sizeof(visit)); 35 for(i=0; i<m; i++) 36 { 37 scanf("%s",map[i]); 38 getchar(); 39 } 40 count = 0; 41 for(i=0; i<m; i++) 42 for(j=0; j<n; j++) 43 { 44 if(!visit[i][j]&&map[i][j]=='@') 45 { 46 count++; 47 DFS(i,j); 48 } 49 } 50 printf("%d\n",count); 51 } 52 return 0; 53 }