We can go through all the nodes in the grid. We can use dfs to traverse each of the connected area from the node that is 1, and we will mark visited for the nodes we traversed.
time: O(m * n) - we will traverse the nodes of all '1'
space: O(m * n) - call stack of all number of nodes in the grid and the visited grid
We can go through all the nodes in the grid. We can use dfs to traverse each of the connected area from the node that is 1, and we will mark visited for the nodes we traversed.
time: O(m * n)
space: O(m * n) - mark visited