It is fishing time🎣 Maximum Fish Catcher! 🐟
🧠 IntuitionThe problem involves exploring all possible paths from any starting water cell (r, c) to maximize the number of fish caught. This can be visualized a
# 🧠 Intuition  

The problem involves exploring all possible paths from any starting water cell `(r, c)` to maximize the number of fish caught. This can be visualized as a **graph traversal problem**, where cells are nodes, and adjacency defines edges.  
We will simulate the fisher's movement using **Breadth-First Search (BFS)** to collect fish in all reachable water cells.  

---

# 🚀 Approach  
1. Traverse the grid to identify potential starting water cells `(r, c)`.  
2. Use BFS from each starting cell to collect all fish in connected water cells.  
3. Track the **maximum fish count** among all possible starting cells.  
4. Mark cells as visited (by setting `grid[r][c] = 0`) during BFS to avoid revisiting them.  

---

# 💻 Complexity Analysis  
- **Time Complexity:**  $$\ O(m \times n)$$  
  We iterate over all cells in the grid and perform BFS from each water cell. Each cell is visited at most once during BFS.  

- **Space Complexity:**  $$\ O(m \times n)$$  
  The BFS queue can grow to the size of the grid in the worst case.  

---
![c1ee5054-dd07-4caa-a7df-e21647cfae9e_1709942227.5165014.png](https://assets.leetcode.com/users/images/8fc06a96-10f4-4cad-a865-3309d16a5016_1738045485.4530952.png)


# 🧑‍💻 Code  

```cpp []
class Solution {
public:
    int findMaxFish(vector<vector<int>>& grid) {
        int res = 0;
        for (int i = 0; i < grid.size(); i++) {
            for (int j = 0; j < grid[0].size(); j++) {
                if (grid[i][j] > 0) {  // Water cell with fish
                    res = max(res, bfs(grid, i, j));
                }
            }
        }
        return res;
    }

private:
    int bfs(vector<vector<int>>& grid, int i, int j) {
        int res = 0;
        queue<pair<int, int>> q;
        q.push({i, j});
        while (!q.empty()) {
            auto [r, c] = q.front(); q.pop();
            if (grid[r][c] == 0) continue;
            res += grid[r][c];
            grid[r][c] = 0;  // Mark cell as visited
            
            vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
            for (auto [dr, dc] : directions) {
                int nr = r + dr, nc = c + dc;
                if (nr >= 0 && nr < grid.size() && nc >= 0 && nc < grid[0].size() && grid[nr][nc] > 0) {
                    q.push({nr, nc});
                }
            }
        }
        return res;
    }
};
```  


```python []
from collections import deque

class Solution:
    def findMaxFish(self, grid):
        def bfs(grid, i, j):
            q = deque([(i, j)])
            total_fish = 0
            while q:
                r, c = q.popleft()
                if grid[r][c] == 0:
                    continue
                total_fish += grid[r][c]
                grid[r][c] = 0  # Mark cell as visited
                for dr, dc in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                    nr, nc = r + dr, c + dc
                    if 0 <= nr < len(grid) and 0 <= nc < len(grid[0]) and grid[nr][nc] > 0:
                        q.append((nr, nc))
            return total_fish
        
        max_fish = 0
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                if grid[i][j] > 0:  # Water cell with fish
                    max_fish = max(max_fish, bfs(grid, i, j))
        return max_fish
```

---

# 🔍 Examples  

### Example 1  
![example.png](https://assets.leetcode.com/users/images/81a7916c-c164-4f39-88dc-6fe2c68b2c44_1738045600.8494244.png)

**Input:**  
```python
grid = [[0, 2, 1, 0], [4, 0, 0, 3], [1, 0, 0, 4], [0, 3, 2, 0]]
```  
**Output:**  
```python
7
```  
**Explanation:**  
- Start at cell `(1, 3)` and catch `3` fish.  
- Move to cell `(2, 3)` and catch `4` fish.  
- Total = `7`.  
> > > > > > >> > > > > > >> > > > > > >> > > > > > > --- 
### Example 2  
![example2.png](https://assets.leetcode.com/users/images/5a31f8ad-5fb6-4d9c-8911-389f505c0800_1738045614.318158.png)

**Input:**  
```python
grid = [[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]]
```  
**Output:**  
```python
1
```  
**Explanation:**  
- Start at cell `(0, 0)` or `(3, 3)` to catch `1` fish.  

---
# Takeaway 🚀

**Thank you for reading! 😊 I hope this solution helps you understand and solve the problem effectively. Happy coding! 🚀**

![1HBC0324_COV.jpg](https://assets.leetcode.com/users/images/e4e20763-ed93-4f6b-bff8-2a4c03a3e68a_1738045764.2778542.jpeg)


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