#include #include #include #include #include #include #include #include #include #pragma GCC optimize("O3") #pragma comment(linker, "STACK:36777216") using namespace std; using i64 = int64_t; constexpr i64 mod = 1e9 + 7; using vi = vector; using vvi = vector; using ii = pair; using vii = vector; int h, w; bool board[3000][3000]; inline bool is_valid(const ii p) { return 0 <= p.first && p.first < h && 0 <= p.second && p.second < w; } void dfs(const ii p) { if (is_valid(p) && board[p.first][p.second]) { board[p.first][p.second] = 0; dfs(ii(p.first + 1, p.second)); dfs(ii(p.first - 1, p.second)); dfs(ii(p.first, p.second + 1)); dfs(ii(p.first, p.second - 1)); } } int cnt; int main() { cin >> h >> w; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> board[i][j]; } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (board[i][j]) { dfs(ii(i, j)); cnt++; } } } cout << cnt << endl; }