DFS from Water Cells | C++, Python, Java
ApproachPerform a DFS starting from every unmarked water cell where you visit every 4-directionally adjacent cells (up, down, left, right). When you visit an ad
# Approach
<!-- Describe your approach to solving the problem. -->
Perform a DFS starting from every unmarked water cell where you visit every 4-directionally adjacent cells (up, down, left, right). When you visit an adjacent water cell, mark it as visited by setting it to zero so that we don't visit it again. After each dfs return the amount of fish we got from every nearby water cell.
# Complexity
- Time complexity: $$O(m \cdot n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(m \cdot n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    int findMaxFish(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size(), res = 0;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (grid[i][j] > 0)
                    res = max(res, dfs(i, j, grid));
        return res;
    }
private:
    int dfs(int r, int c, vector<vector<int>>& grid){
        if (r < 0 || c < 0 || r == grid.size() || c == grid[0].size() || grid[r][c] == 0)
            return 0;
        int res = grid[r][c];
        grid[r][c] = 0;
        return res + dfs(r + 1, c, grid) + dfs(r - 1, c, grid) + dfs(r, c + 1, grid) + dfs(r, c -  1, grid);
    }
};
```
```python []
class Solution:
    def findMaxFish(self, grid: List[List[int]]) -> int:
        m, n = len(grid), len(grid[0])

        def dfs(r, c):
            if r < 0 or r == m or c < 0 or c == n or grid[r][c] == 0:
                return 0
            total = grid[r][c]
            grid[r][c] = 0
            return total + dfs(r + 1, c) + dfs(r - 1, c) + dfs(r, c + 1) + dfs(r, c - 1)
        
        return max(dfs(r, c) if grid[r][c] > 0 else 0 for r in range(m) for c in range(n))
```
```java []
class Solution {
    public int findMaxFish(int[][] grid) {
        int res = 0, m = grid.length, n = grid[0].length;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (grid[i][j] > 0)
                    res = Math.max(res, dfs(i, j, grid));
        return res;
    }
    private int dfs(int r, int c, int[][] grid){
        if (r < 0 || c < 0 || r == grid.length || c == grid[0].length || grid[r][c] == 0)
            return 0;
        int res = grid[r][c];
        grid[r][c] = 0;
        return res + dfs(r + 1, c, grid) + dfs(r - 1, c, grid) + dfs(r, c + 1, grid) + dfs(r, c -  1, grid);
    }
}
```
['Array', 'Depth-First Search', 'Breadth-First Search', 'Union Find', 'C', 'Matrix', 'Python', 'C++', 'Java', 'Python3']