Simple solution Python3
IntuitionThe problem requires finding the maximum number of fish in any connected component of a 2D grid. A connected component is defined by adjacent (up, down
# Intuition
The problem requires finding the maximum number of fish in any connected component of a 2D grid. A connected component is defined by adjacent (up, down, left, right) cells with positive values. The goal is to traverse the grid and calculate the total fish count for each connected component, keeping track of the maximum value encountered.

# Approach
1. **Depth-First Search (DFS)**:
   - Use DFS to explore connected components in the grid.
   - For each unvisited cell containing fish (`grid[row][col] > 0`), traverse all connected cells to calculate the total fish count.

2. **Tracking Visited Cells**:
   - Maintain a `visited` matrix to ensure cells are not counted multiple times.
   - Mark a cell as visited during the DFS to avoid revisiting it.

3. **Iterate Through the Grid**:
   - For each cell in the grid, if it contains fish and has not been visited, initiate a DFS to compute the total fish count for the connected component starting from that cell.
   - Update the maximum fish count if the current component's total fish is greater.

# Complexity
- **Time Complexity**:  
  $$O(m \times n)$$, where $$m$$ is the number of rows and $$n$$ is the number of columns in the grid.  
  Each cell is visited at most once, and the DFS explores all connected cells in linear time relative to the total number of cells.

- **Space Complexity**:  
  $$O(m \times n)$$ for the `visited` matrix. Additionally, the recursive DFS stack may use up to $$O(m \times n)$$ space in the worst case (if the entire grid is a single connected component).

# Code
```python
class Solution:
    # Helper function to count the number of fishes in a connected component
    def calculate_fishes(self, grid, visited, row, col):
        # Check boundary conditions, water cells, or already visited cells
        if (
            row < 0
            or row >= len(grid)
            or col < 0
            or col >= len(grid[0])
            or grid[row][col] == 0
            or visited[row][col]
        ):
            return 0

        # Mark the current cell as visited
        visited[row][col] = True

        # Accumulate the fish count from the current cell and its neighbors
        return (
            grid[row][col]
            + self.calculate_fishes(grid, visited, row, col + 1)
            + self.calculate_fishes(grid, visited, row, col - 1)
            + self.calculate_fishes(grid, visited, row + 1, col)
            + self.calculate_fishes(grid, visited, row - 1, col)
        )

    def findMaxFish(self, grid):
        rows, cols = len(grid), len(grid[0])
        max_fish_count = 0

        # A 2D list to track visited cells
        visited = [[False] * cols for _ in range(rows)]

        # Iterate through all cells in the grid
        for row in range(rows):
            for col in range(cols):
                # Start a DFS for unvisited land cells (fish available)
                if grid[row][col] > 0 and not visited[row][col]:
                    max_fish_count = max(
                        max_fish_count,
                        self.calculate_fishes(grid, visited, row, col),
                    )

        # Return the maximum fish count found
        return max_fish_count
```

# Example
**Input:**  
```python
grid = [
    [4, 0, 2],
    [0, 3, 0],
    [1, 0, 5]
]
```

**Output:**  
`9`

**Explanation:**  
- The first connected component has 4 fish (at `(0, 0)`).
- The second connected component has 9 fish (at `(0, 2)` and `(2, 2)` connected diagonally via `(1, 1)`).
- The third connected component has 1 fish (at `(2, 0)`).
- The maximum fish count is `9`.

This solution is efficient and well-suited for handling grid-based problems.
['Python3']