#include using namespace std; typedef long long ll; vector> field; vector x; void dfs(int y, int x){ field[y][x] = 0; if (field[y+1][x]) dfs(y+1, x); if (field[y-1][x]) dfs(y-1, x); if (field[y][x+1]) dfs(y, x+1); if (field[y][x-1]) dfs(y, x-1); return; } int main(){ int h, w, k, counter; cin >> h >> w; counter = 0; for (int i = 0; i < w + 2; i++) x.push_back(0); field.push_back(x); x.clear(); for (int i = 0; i < h; i++){ x.push_back(0); for (int j = 0; j < w; j++){ cin >> k; x.push_back(k); } x.push_back(0); field.push_back(x); x.clear(); } for (int i = 0; i < w + 2; i++) x.push_back(0); field.push_back(x); // for (auto v : field){ // for (auto w : v) // cout << w; // cout << endl; // } for (int i = 0; i < h; i++){ for (int j = 0; j < w; j++){ if (field[i+1][j+1]){ dfs(i+1, j+1); counter++; } } } // for (auto v : field){ // for (auto w : v) // cout << w; // cout << endl; // } cout << counter << endl; return 0; }