#include #include using namespace std; int main() { int ans = 0; int h, w; cin >> h >> w; int masu[h][w]; for(int i=0; i> masu[i][j]; stack stkH; stack stkW; for(int i=0; i= 0 && masu[a][b-1]){ masu[a][b-1] = 0; stkH.push(a); stkW.push(b-1); } if(a - 1 >= 0 && masu[a - 1][b]){ masu[a - 1][b] = 0; stkH.push(a-1); stkW.push(b); } if(a + 1 < h && masu[a + 1][b]){ masu[a + 1][b] = 0; stkH.push(a+1); stkW.push(b); } if(!stkH.empty()){ a = stkH.top(); b = stkW.top(); stkH.pop(); stkW.pop(); } else break; } } cout << ans << endl; return 0; }