#include "bits/stdc++.h" #define REP(i,n,N) for(int i=(n); i<(N); i++) #define RREP(i,n,N) for(ll i=(N-1); i>=n; i--) #define CK(n,a,b) ((a)<=(n)&&(n)<(b)) #define ALL(v) (v).begin(),(v).end() #define p(s) cout<<(s)<> typedef long long ll; using namespace std; const ll mod= 1e10; int dx[4] = { 0, 1, 0, -1 }; int dy[4] = { -1, 0, 1, 0 }; int H; //フィールドの高さ int W; //フィールドの幅 int start_y, start_x; //開始座標 const int NODE_SIZE = 3010; int field[NODE_SIZE][NODE_SIZE]; //格子状のフィールド (string) //幅優先探索 (フィールド: 配列/string) void bfs_field() { queue> q; //<座標(y,x)> q.push({start_y, start_x}); //開始ノードを追加 while(!q.empty()){ int cur_y = q.front().first; int cur_x = q.front().second; q.pop(); if(field[cur_y][cur_x]==0) continue; field[cur_y][cur_x]=0; REP(k,0,4){ int next_y = cur_y + dy[k]; int next_x = cur_x + dx[k]; if(!CK(next_y,0,H) || !CK(next_x,0,W)) continue; //範囲外 /* ここに問題ごとの条件 */ if(field[next_y][next_x]){ //未到達の座標だけpush. q.push({next_y, next_x}); } } } } int main(){ cin>>H>>W; REP(i,0,H) { REP(j, 0, W) { cin >> field[i][j]; } } int ans=0; REP(i,0,H){ REP(j,0,W){ if(field[i][j]){ start_y = i; start_x = j; bfs_field(); ans++; } } } p(ans); return 0; }