How would you solve the number of islands problem in a 2D grid?
Implement num_islands(grid), where grid is a rectangular list of lists containing only the strings '1' and '0'. Land cells are connected only horizontally or vertically, not diagonally. Return the number of distinct islands without changing the input grid.
def num_islands(grid):