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
| #include <bits/stdc++.h> using namespace std; template <typename _Tp> inline void read(_Tp &x){ int w=1;char c=0;x=0; while (c^'-'&&(c<'0'||c>'9'))c=getchar(); if (c=='-')w=-1,c=getchar(); while (c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar(); x*=w; } inline void write(int n){ if(n==0) return; write(n/10); putchar(n%10+'0'); } int n,m,ans,dx[]={-1,0,1,0},dy[]={0,-1,0,1},a[105][105]; void dfs(int x,int y){ if (x>n||y>m||x<0||y<0)return; a[x][y]=0; for (int i=0;i<4;i++)if (a[x+dx[i]][y+dy[i]])dfs(x+dx[i],y+dy[i]); } int main(){ read(n),read(m); for (int i=0;i<n;i++) for (int j=0;j<m;j++)scanf("%1d",&a[i][j]); for (int i=0;i<n;i++) for (int j=0;j<m;j++)if (a[i][j])ans++,dfs(i,j); write(ans); return 0; }
|