#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define sz size() #define pb push_back #define mp make_pair #define fi first #define se second #define all(c) (c).begin(), (c).end() #define rep(i,a,b) for(ll i=(a);i<(b);++i) #define per(i,a,b) for(ll i=(b-1);i>=(a);--i) #define clr(a, b) memset((a), (b) ,sizeof(a)) #define ctos(c) string(1,c) #define print(x) cout<<#x<<" = "<>h>>w; rep(y,0,h){ rep(x,0,w){ int a; scanf("%d",&a); d[y][x] = a; } } int ans = 0; short dy[4] = {0,0,-1,1}; short dx[4] = {-1,1,0,0}; rep(y,0,h){ rep(x,0,w){ if(d[y][x]==0)continue; ans++; queue q1; queue q2; q1.push(y); q2.push(x); while(!q1.empty()){ short y1 = q1.front();q1.pop(); short x1 = q2.front();q2.pop(); d[y1][x1] = 0; rep(i,0,4){ short y2 = y1 + dy[i]; short x2 = x1 + dx[i]; if(y2<0)continue; if(y2>=h)continue; if(x2<0)continue; if(x2>=w)continue; if(d[y2][x2]==0)continue; q1.push(y2); q2.push(x2); } } } } cout << ans << endl; return 0; }