Approach hintTry it first, then revealHide
Label every island with a DSU or DFS and record its size. For each water cell, add the sizes of the distinct islands around it plus one, and remember the case where the grid has no water at all.
Label every island with a DSU or DFS and record its size. For each water cell, add the sizes of the distinct islands around it plus one, and remember the case where the grid has no water at all.