Explain how you would implement a depth-first search on a grid and handle visited cells and boundaries.
Implement num_islands(grid), returning the number of four-directionally connected regions containing 1. Treat 0 as empty, do not count diagonal connections, and leave the input unchanged.
Input is a list of equally sized lists of integers. Return an integer. Return 0 for an empty grid.
def num_islands(grid):