Problem Description

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.

Input

The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m=0m = 0 it signals the end of the input; otherwise 1<=m<=1001 <= m <= 100 and 1<=n<=1001 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either ’ * ’ , representing the absence of oil, or ’ @ ', representing an oil pocket.

Output

For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100100 pockets.

Sample Input

1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0

Sample Output

0
1
2
2

Translation

一张地图由’*‘和’@‘组成,每一个’@‘代表一块油田,如果两个’@'相邻,那么可以拼成一块更大的油田,问一共有多少块独立的油田。

Idea

显然是并查集的思想,遍历每一个’@‘,将相邻的’@'放入同一个集合。
注意:观察样例,相邻指上、下、左、右、左上、右上、左下、右下八个方向。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include<iostream>
#include<cstring>
using namespace std;
const int N=104;
struct node
{
int x;
int y;
};
//位移矢量
const int dx[]={-1,1,0,0,-1,-1,1,1};
const int dy[]={0,0,-1,1,-1,1,-1,1};
int m,n;
int cnt;//油田的数量
int id[N][N];//储存地图上油田的编号
node oil[N*N];//储存油田坐标
char map[N][N];//储存地图
int father[N*N];//储存父亲
void init()//初始化
{
memset(father,0,sizeof(father));
cnt=0;
}
int find(int x)
{
if(x==father[x]) return x;
else return father[x]=find(father[x]);
}
void unions(int a,int b)
{
a=find(a);
b=find(b);
if(a==b) return;
else
{
father[b]=a;
return;
}
}
void solve()
{
init();
int i,j;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
cin>>map[i][j];
if(map[i][j]=='@')
{
cnt++;
//记录油田的坐标
oil[cnt].x=i;
oil[cnt].y=j;
id[i][j]=cnt;//记录每一个位置上的油田的序号
father[cnt]=cnt;
}
}
}
for(i=1;i<=cnt;i++)//遍历每一个油田
{
node now,nxt;
now.x=oil[i].x;
now.y=oil[i].y;
int j;
for(j=0;j<8;j++)//搜索八个方向
{
nxt.x=now.x+dx[j];
nxt.y=now.y+dy[j];
if(nxt.x<1||nxt.y<1||nxt.x>m||nxt.y>n) continue;
if(map[nxt.x][nxt.y]=='@')
{
//将相邻油田放入同一个集合
unions(i,id[nxt.x][nxt.y]);
}
}
}
int ans=0;
//统计集合的个数
for(i=1;i<=cnt;i++)
{
if(father[i]==i)
{
ans++;
}
}
cout<<ans<<endl;
}
int main()
{
while(cin>>m>>n&&m) solve();
return 0;
}