Python | Simple DFS | O(m*n), O(m*n) | Beats 95%
CodeComplexity

Time complexity: O(m∗n). Visit each cell once, m*n total cells.


Space complexity: O(m∗n). Based on maximum recursion depth of m*n total cells.
# Code
```python3 []
class Solution:
    def findMaxFish(self, grid: List[List[int]]) -> int:
        def collect_fish(i, j):
            num_fish = grid[i][j] # collect fish from current cell
            grid[i][j] = 0 # caught fish in current cell, no more remaining

            for di, dj in [(0, 1), (0, -1), (1, 0), (-1, 0)]: # explore adjacent cells
                ni, nj = i + di, j + dj
                if 0 <= ni < m and 0 <= nj < n and grid[ni][nj] != 0: 
                    num_fish += collect_fish(ni, nj) # if cell in bounds and has fish, collect

            return num_fish

        m = len(grid)
        n = len(grid[0])
        max_fish = 0
        for i in range(m): # loop through each cell and update max fish if cell's section has more
            for j in range(n):
                if grid[i][j] == 0: continue # skip if cell has no fish
                max_fish = max(max_fish, collect_fish(i, j))

        return max_fish
```

# Complexity
- Time complexity: $$O(m*n)$$. Visit each cell once, m*n total cells.

- Space complexity: $$O(m*n)$$. Based on maximum recursion depth of m*n total cells.

# Performance

![Screenshot (832).png](https://assets.leetcode.com/users/images/bf4fd8f4-0ae9-47c4-8bdf-3c8b1ba28631_1738025467.399183.png)
['Array', 'Depth-First Search', 'Matrix', 'Python3']