#include using namespace std; typedef long long ll; const int MOD = 1000000007; const int MAX_N = 3010; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; #define REP(i,n) for((i)=0;(i)<(int)(n);(i)++) int field[MAX_N][MAX_N]; int h, w; typedef pair P; void dfs(int x, int y){ stack

s; s.push(P(x, y)); while(s.size()){ P p = s.top(); s.pop(); field[p.first][p.second] = 0; for (int i = 0; i < 4; i++){ int nx = p.first + dx[i]; int ny = p.second + dy[i]; if (0 <= nx && nx < h && 0 <= ny && ny < w && field[nx][ny]){ s.push(P(nx, ny)); } } } } int main(){ cin >> h >> w; int i, j; REP(i, h) REP(j, w) cin >> field[i][j]; int ans = 0; REP(i, h) REP(j, w){ if (field[i][j]){ dfs(i, j); ans++; } } cout << ans << endl; return 0; }