Maximum fish using DFS
IntuitionThe problem can be thought of as finding the connected components in a 2D grid where the grid cells represent fish counts in a pond. The goal is to fin
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem can be thought of as finding the connected components in a 2D grid where the grid cells represent fish counts in a pond. The goal is to find the maximum sum of fish in any connected component (group of cells). This is a classic grid traversal problem that can be solved using Depth First Search (DFS).
# Approach
<!-- Describe your approach to solving the problem. -->
### Initialization:
1. **Determine the dimensions of the grid**:
   - Let `m` represent the number of rows and `n` represent the number of columns in the grid.

2. **Create a `vis` array**:
   - Use a boolean `vis` array of size `m x n` to track visited cells and avoid revisiting them during the DFS.

---

### Depth First Search (DFS):
1. **Condition to initiate DFS**:
   - For a given cell `(i, j)`, if:
     - `grid[i][j] != 0` (cell contains fish), and
     - `vis[i][j] == false` (cell is not visited),
   - Then initiate a DFS from that cell.

2. **DFS traversal**:
   - Accumulate the total fish count for the connected component by recursively exploring its 4 neighboring cells (up, down, left, right).

3. **Mark cell as visited**:
   - During each step, mark the current cell `(i, j)` as visited by setting `vis[i][j] = true`.

---

### Iterate Through the Grid:
1. Traverse every cell in the grid:
   - For each cell `(i, j)` that satisfies the DFS initiation condition, compute the fish count for its connected component using the `solve` (DFS) function.

2. Keep track of the maximum fish count:
   - Compare the fish count of the current connected component with the maximum fish count encountered so far, and update the maximum.

---

### Return the Result:
- After iterating through all cells in the grid, return the maximum fish count as the result.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
Each cell is visited at most once during the DFS traversal, so the total time complexity is **O(m×n)**, where m is the number of rows and n is the number of columns in the grid.


# Code
```java []
class Solution {
    int m, n;
    public int solve(int i, int j, int[][] grid, boolean[][] vis) {
        if (i < 0 || i >= m || j < 0 || j >= n || vis[i][j] || grid[i][j] == 0) return 0;
        int temp = grid[i][j];
        vis[i][j] = true;
        temp += solve(i + 1, j, grid, vis);
        temp += solve(i - 1, j, grid, vis);
        temp += solve(i, j + 1, grid, vis);
        temp += solve(i, j - 1, grid, vis);
        return temp;
    }
    public int findMaxFish(int[][] grid) {
        m = grid.length;
        n = grid[0].length;
        int ans = 0;
        boolean[][] vis = new boolean[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] != 0 && !vis[i][j]) {
                    ans = Math.max(ans, solve(i, j, grid, vis));
                }
            }
        }
        return ans;
    }
}
```
['Array', 'Depth-First Search', 'Matrix', 'Java']