land: 1 is land and 0 is water. An island is a group of land cells connected through their sides (up, down, left, right); diagonal contact does not count.
You have enough soil for exactly one cell: you may turn at most one water cell into land (or change nothing).
Return the size (number of cells) of the largest island you can end up with.land=[[1, 1, 0], [0, 0, 1], [1, 1, 1]] → 7
There are islands of size 2 (top left) and 4. Filling the centre cell joins both of them: 2 + 4 + 1 = 7.
land=[[1, 0, 1], [0, 0, 0], [1, 0, 1]] → 3
Four one-cell islands. Filling the cell between two neighbouring corners gives an island of 3 cells.
land=[[0, 0], [0, 0]] → 1
There is no land at all, but filling one cell creates an island of size 1.
land=[[1, 1, 0], [0, 0, 1], [1, 1, 1]]
7
Run checks the first 3 tests in your browser. Submit checks all 83 tests on the server, hidden ones included.