Simple C++ Approach ✅ | Beats 100%🔥
Intuition 🧐The problem can be visualized as finding the maximum sum of connected components in a 2D grid where each cell represents the number of fish. A BFS or
# Intuition 🧐
The problem can be visualized as finding the maximum sum of connected components in a 2D grid where each cell represents the number of fish. A BFS or DFS traversal allows us to explore all connected cells efficiently, while keeping track of the sum for each connected region.

# Approach 🚀
1) Initialization:
- Define direction vectors to navigate the grid in 4 directions (up, down, left, right).
- Create a helper function valid to check if a given cell is within bounds.
2) Traversal:
- Iterate through each cell in the grid.
- If a cell contains fish (grid[i][j] != 0), perform a BFS to explore the connected component:
----> Add the current cell’s fish count to a running sum.
----> Use a queue to process all reachable cells, marking visited cells as 0 to avoid revisiting.
- Compare the sum of the current component with the global maxSum and update it if necessary.
3) Output:
Return the maximum fish sum found across all connected components.


# Complexity ⌛
- Time complexity:
**O(r⋅c)**, 
where 𝑟 is the number of rows and 𝑐 is the number of columns. Each cell is visited once during the BFS/DFS.

- Space complexity:
**O(min(r,c))**,
due to the BFS queue storing at most one layer of the grid (worst-case linear space usage).

# Code 📝
```cpp []
class Solution {
public:
    int row[4] = {0,0,1,-1};
    int col[4] = {1,-1,0,0};

    bool valid(int x,int y,int r,int c)
    {
        return x>=0&&x<r&&y>=0&&y<c;
    }

    int findMaxFish(vector<vector<int>>& grid) {
        int r = grid.size();
        int c = grid[0].size();

        queue<pair<int,int>> q;
        int sum = 0;

        int maxSum = 0;
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                if(grid[i][j] != 0)
                {
                    sum=0;   //**reset sum for each BFS
                    sum+=grid[i][j];
                    grid[i][j] = 0;
                    q.push({i,j});

                    while(!q.empty())
                    {
                        int r1 = q.front().first;
                        int c1 = q.front().second;
                        q.pop();

                        //check all 4 directions
                        for(int k=0;k<4;k++)
                        {
                            if(valid(r1+row[k],c1+col[k],r,c) && grid[r1+row[k]][c1+col[k]] != 0)
                            {
                                sum+=grid[r1+row[k]][c1+col[k]];
                                grid[r1+row[k]][c1+col[k]] = 0;
                                q.push({r1+row[k],c1+col[k]});
                            }
                        }
                    }
                }
                maxSum = max(sum,maxSum);
            }
        }

        return maxSum;
    }
};
```

![Upvote! It only takes 1 click😉](https://assets.leetcode.com/users/images/48b755ae-c41b-474d-966e-aa5674e41ff4_1720943081.9620614.jpeg)
['Breadth-First Search', 'Graph', 'Matrix', 'C++']