#include using namespace std; #define rep(i,a,b) for(int i=a;i> h >> w; int arr[h][w]; int ans = 0; rep(i,0,h){ rep(j,0,w){ cin >> arr[i][j]; } } rep(i,0,h){ rep(j,0,w){ if(arr[i][j] == 1){ stack> stk; stk.push(make_pair(i,j)); while(!stk.empty()){ pair tmp = stk.top(); arr[tmp.first][tmp.second] = 0; stk.pop(); rep(k,0,4){ int y = tmp.first + dy[k]; int x = tmp.second + dx[k]; if(-1 < y && y < h && -1 < x && x < w && arr[y][x] == 1) stk.push(make_pair(y,x)); } } ans++; } } } cout << ans << endl; }