C++ Solution || Simple BFS
IntuitionThe problem can be approached by treating the grid as a graph where:
Each cell is a node.
Edges exist between adjacent cells that are non-zero.
The tas
# Intuition
The problem can be approached by treating the grid as a graph where:
- Each cell is a node.
- Edges exist between adjacent cells that are non-zero.

The task is to find all connected components (regions) of the grid and calculate the sum of fish in each component. The largest sum is the desired answer.

# Algorithm
1. Use Breadth-First Search (BFS) to traverse each region of connected cells starting from a non-zero cell.
2. For each region:
   - Add up the number of fish.
   - Mark cells as visited by setting them to `0` (water) to avoid revisiting.
3. Track the maximum sum of fish collected across all regions.

---

# Complexity

- **Time Complexity**:  
  $$O(m \cdot n)$$  
  Each cell is visited at most once during the BFS traversal.

- **Space Complexity**:  
  $$O(m \cdot n)$$  
  In the worst case, the BFS queue could store all cells in the grid.

---


# Code
```cpp
class Solution {
public:
    int bfs(vector<vector<int>>& grid, int r, int c) {
        int m = grid.size(), n = grid[0].size();
        queue<pair<int, int>> q;
        q.push({r, c});
        int totalFish = 0;
        int dr[] = {-1, 0, 0, 1};
        int dc[] = {0, -1, 1, 0};
        while (!q.empty()) {
            int row = q.front().first, col = q.front().second;
            q.pop();
            totalFish += grid[row][col];
            grid[row][col] = 0; 
            for (int i = 0; i < 4; i++) {
                int nr = row + dr[i], nc = col + dc[i];
                if (nr >= 0 && nr < m && nc >= 0 && nc < n && grid[nr][nc] > 0) {
                    q.push({nr, nc});
                }
            }
        }

        return totalFish;
    }

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

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] > 0) {
                    maxFish = max(maxFish, bfs(grid, i, j));
                }
            }
        }

        return maxFish;
    }
};
````
![d3550b79-52c8-4704-846f-c00daecb5632_1737784461.667478.png](https://assets.leetcode.com/users/images/374fe02b-e3ac-411b-8095-4ad60087e264_1738050423.486502.png)

['Array', 'Breadth-First Search', 'Matrix', 'C++']