terrain with m rows and n columns. Each cell is the string '1' (land) or '0' (water).
A land region is a group of '1' cells connected through horizontal or vertical neighbours; touching diagonally does not count. Everything outside the grid is water.
Return the number of separate land regions.terrain=[['1', '0', '1'], ['1', '0', '0'], ['0', '1', '1']] → 3
Three regions: the two '1's in the left column, the lone '1' in the top-right corner and the pair at the bottom. Cells touching only diagonally are not joined.
terrain=[['1', '1', '1'], ['1', '0', '1'], ['1', '1', '1']] → 1
The ring of land around a single water cell is one region.
terrain=[['0', '0']] → 0
terrain=[['1', '0', '1'], ['1', '0', '0'], ['0', '1', '1']]
3
Run checks the first 3 tests in your browser. Submit checks all 62 tests on the server, hidden ones included.