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