Easy C++ solution
IntuitionThe problem involves finding the maximum sum of connected non-zero cells in a grid. This suggests a graph traversal problem, where each non-zero cell i
# Intuition
The problem involves finding the maximum sum of connected non-zero cells in a grid. This suggests a **graph traversal problem**, where each non-zero cell is a node, and edges exist between adjacent non-zero cells (up, down, left, right). We can treat the problem as finding the largest **connected component** in the grid.

Depth-First Search (DFS) is well-suited for exploring connected components in a grid-based problem.

---

# Approach
1. **DFS Traversal**: 
   - Start DFS from each unvisited non-zero cell.
   - Explore all four possible directions (up, down, left, right).
   - Mark cells as visited to prevent revisiting.
   - Sum the values of all connected non-zero cells.

2. **Iterate Over the Grid**: 
   - For each unvisited non-zero cell, initiate DFS to compute the sum of connected non-zero values.
   - Keep track of the maximum sum encountered.

---

# Complexity
- **Time Complexity**:  
  Each cell is visited once, and each DFS call processes up to 4 directions. In the worst case, all cells are part of one large connected component, leading to an $$O(m \times n)$$ complexity.

- **Space Complexity**:  
  - **Recursive Stack**: In the worst case, the DFS recursion depth could reach $$O(m \times n)$$ in the case of a single large connected component.
  - **Visited Array**: Requires $$O(m \times n)$$ space.
  - **Total Space Complexity**: $$O(m \times n)$$

---

# Code
```cpp
class Solution {
public:
    int dfs(int r, int c, vector<vector<int>> &grid, vector<vector<bool>> &visited) {
        int m = grid.size(), n = grid[0].size();
        if (r < 0 || r >= m || c < 0 || c >= n || grid[r][c] == 0 || visited[r][c])
            return 0;
        
        visited[r][c] = true;
        int sum = grid[r][c]; // Collect fish from the current cell

        // Explore all 4 possible directions
        sum += dfs(r + 1, c, grid, visited);
        sum += dfs(r - 1, c, grid, visited);
        sum += dfs(r, c + 1, grid, visited);
        sum += dfs(r, c - 1, grid, visited);
        
        return sum;
    }

    int findMaxFish(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size();
        vector<vector<bool>> visited(m, vector<bool>(n, false));
        int maxFish = 0;

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] > 0 && !visited[i][j]) {
                    maxFish = max(maxFish, dfs(i, j, grid, visited));
                }
            }
        }
        
        return maxFish;
    }
};
```
['Depth-First Search', 'C++']