#include enum State { None = -1, Field, Lake, Lake_Check, }; bool ChechLake(short x, short y, char *s, const int *h, const int *w) { if(s[x + y * (*w)] != (char)State::Lake) return false; s[x + y * (*w)] = (char)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; char *s = new char[h * w]; for(int i = 0; i < (h * w); i++) { int a; std::cin >> a; s[i] = (char)a; } int count = 0; for(short y = 0; y < h; y++) { for(short x = 0; x < w; x++) { if(ChechLake(x, y, s, &h, &w)) count++; } } std::cout << count << std::endl; delete[] s; return 0; }