#include enum State { None = -1, Field, Lake, Lake_Check, }; bool ChechLake(int x, int y, int *s, int h, int w) { if(s[x + y * w] != State::Lake) return false; s[x + y * w] = State::Lake_Check; if(x < (w - 1)) ChechLake(x + 1, y, s, h, w); if(x > 0) ChechLake(x - 1, y, s, h, w); if(y < (h - 1)) ChechLake(x, y + 1, s, h, w); if(y > 0) ChechLake(x, y - 1, s, h, w); return true; } int main() { int h, w; std::cin >> h >> w; int *s = new int[h * w]; for(int i = 0; i < (h * w); i++) { std::cin >> s[i]; } int count = 0; for(int y = 0; y < h; y++) { for(int x = 0; x < w; x++) { if(ChechLake(x, y, s, h, w)) count++; } } std::cout << count << std::endl; delete[] s; return 0; }