🌟BFS for Beginners 💡 | 🎉 Effortless grid exploration with BFS 🧩.
Intuition 🧠
Find the largest group of connected cells with fish using BFS to calculate the total fish in each group. Track the maximum fish among all groups.
Ap
# Intuition 🧠
- Find the largest group of connected cells with fish using `BFS` to calculate the `total` fish in each group. Track the maximum fish among all groups.

# Approach 🚀
1. **Traverse Grid:** For each unvisited cell with fish, `start a BFS`.
2. **BFS Exploration:** Use a queue to explore all connected fish cells, summing their values. Mark visited cells in a `vis matrix`.
3. **Update Maximum:** Compare the current group's fish total (currentfish) with the global max (ans).
4. **Return Result:** After checking all cells, return the `largest fish total.`

# Complexity 📊
     Time: -> O(m * n)
     (Each cell is processed once).

     Space: -> O(m * n)
     (Visited matrix + BFS queue).


# Code
```cpp []
class Solution {
public:
    int findMaxFish(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();
        int ans = 0;
        vector<vector<int>> vis(m, vector<int>(n, 0));

        queue<pair<int, int>> q;

        int delrow[] = {-1, 0, 1, 0};
        int delcol[] = {0, 1, 0, -1};

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {

                if (grid[i][j] > 0 && vis[i][j] == 0) {
                    int currentfish = 0;
                    q.push({i, j});
                    vis[i][j] = 1;

                    while (!q.empty()) {
                        int row = q.front().first;
                        int col = q.front().second;

                        currentfish += grid[row][col];
                        q.pop();

                        for (int d = 0; d < 4; d++) {
                            int nrow = row + delrow[d];
                            int ncol = col + delcol[d];

                            if (nrow >= 0 && nrow < m && ncol >= 0 && ncol < n)
                                if (vis[nrow][ncol] == 0 &&
                                    grid[nrow][ncol] > 0) {
                                    vis[nrow][ncol] = 1;
                                    q.push({nrow, ncol});
                                }
                        }
                    }
                    ans = max(currentfish, ans);
                }
            }
        }

        return ans;
    }
};
```
```java []
import java.util.*;

class Solution {
    public int findMaxFish(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int ans = 0;
        boolean[][] vis = new boolean[m][n];
        
        int[] delrow = {-1, 0, 1, 0};
        int[] delcol = {0, 1, 0, -1};

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] > 0 && !vis[i][j]) {
                    int currentFish = 0;
                    Queue<int[]> q = new LinkedList<>();
                    q.offer(new int[]{i, j});
                    vis[i][j] = true;

                    while (!q.isEmpty()) {
                        int[] cell = q.poll();
                        int row = cell[0], col = cell[1];
                        currentFish += grid[row][col];

                        for (int d = 0; d < 4; d++) {
                            int nrow = row + delrow[d], ncol = col + delcol[d];
                            if (nrow >= 0 && nrow < m && ncol >= 0 && ncol < n && !vis[nrow][ncol] && grid[nrow][ncol] > 0) {
                                vis[nrow][ncol] = true;
                                q.offer(new int[]{nrow, ncol});
                            }
                        }
                    }
                    ans = Math.max(currentFish, ans);
                }
            }
        }
        return ans;
    }
}

```
```python []
from collections import deque

class Solution:
    def findMaxFish(self, grid):
        m, n = len(grid), len(grid[0])
        ans = 0
        vis = [[False] * n for _ in range(m)]

        delrow = [-1, 0, 1, 0]
        delcol = [0, 1, 0, -1]

        for i in range(m):
            for j in range(n):
                if grid[i][j] > 0 and not vis[i][j]:
                    current_fish = 0
                    q = deque([(i, j)])
                    vis[i][j] = True

                    while q:
                        row, col = q.popleft()
                        current_fish += grid[row][col]

                        for d in range(4):
                            nrow, ncol = row + delrow[d], col + delcol[d]
                            if 0 <= nrow < m and 0 <= ncol < n and not vis[nrow][ncol] and grid[nrow][ncol] > 0:
                                vis[nrow][ncol] = True
                                q.append((nrow, ncol))

                    ans = max(current_fish, ans)

        return ans

```
```ruby []
class Solution
  def findMaxFish(grid)
    m, n = grid.length, grid[0].length
    ans = 0
    vis = Array.new(m) { Array.new(n, false) }

    delrow = [-1, 0, 1, 0]
    delcol = [0, 1, 0, -1]

    for i in 0...m
      for j in 0...n
        if grid[i][j] > 0 && !vis[i][j]
          current_fish = 0
          q = [[i, j]]
          vis[i][j] = true

          while !q.empty?
            row, col = q.shift
            current_fish += grid[row][col]

            for d in 0...4
              nrow, ncol = row + delrow[d], col + delcol[d]
              if nrow >= 0 && nrow < m && ncol >= 0 && ncol < n && !vis[nrow][ncol] && grid[nrow][ncol] > 0
                vis[nrow][ncol] = true
                q.push([nrow, ncol])
              end
            end
          end
          ans = [current_fish, ans].max
        end
      end
    end

    return ans
  end
end

```
```javascript []
var findMaxFish = function(grid) {
    const m = grid.length, n = grid[0].length;
    let ans = 0;
    const vis = Array.from({length: m}, () => Array(n).fill(false));

    const delrow = [-1, 0, 1, 0];
    const delcol = [0, 1, 0, -1];

    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (grid[i][j] > 0 && !vis[i][j]) {
                let currentFish = 0;
                let q = [[i, j]];
                vis[i][j] = true;

                while (q.length) {
                    const [row, col] = q.shift();
                    currentFish += grid[row][col];

                    for (let d = 0; d < 4; d++) {
                        const nrow = row + delrow[d], ncol = col + delcol[d];
                        if (nrow >= 0 && nrow < m && ncol >= 0 && ncol < n && !vis[nrow][ncol] && grid[nrow][ncol] > 0) {
                            vis[nrow][ncol] = true;
                            q.push([nrow, ncol]);
                        }
                    }
                }
                ans = Math.max(currentFish, ans);
            }
        }
    }

    return ans;
};

```
```rust []
use std::collections::VecDeque;

impl Solution {
    pub fn find_max_fish(grid: Vec<Vec<i32>>) -> i32 {
        let m = grid.len();
        let n = grid[0].len();
        let mut ans = 0;
        let mut vis = vec![vec![false; n]; m];

        let delrow = [-1, 0, 1, 0];
        let delcol = [0, 1, 0, -1];

        for i in 0..m {
            for j in 0..n {
                if grid[i][j] > 0 && !vis[i][j] {
                    let mut current_fish = 0;
                    let mut q = VecDeque::new();
                    q.push_back((i, j));
                    vis[i][j] = true;

                    while let Some((row, col)) = q.pop_front() {
                        current_fish += grid[row][col];

                        for d in 0..4 {
                            let nrow = row as i32 + delrow[d];
                            let ncol = col as i32 + delcol[d];
                            if nrow >= 0 && nrow < m as i32 && ncol >= 0 && ncol < n as i32
                                && !vis[nrow as usize][ncol as usize] && grid[nrow as usize][ncol as usize] > 0 {
                                vis[nrow as usize][ncol as usize] = true;
                                q.push_back((nrow as usize, ncol as usize));
                            }
                        }
                    }
                    ans = ans.max(current_fish);
                }
            }
        }

        ans
    }
}

```
```typescript []
function findMaxFish(grid: number[][]): number {
    const m = grid.length, n = grid[0].length;
    let ans = 0;
    const vis = Array.from({ length: m }, () => Array(n).fill(false));

    const delrow = [-1, 0, 1, 0];
    const delcol = [0, 1, 0, -1];

    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (grid[i][j] > 0 && !vis[i][j]) {
                let currentFish = 0;
                let q: [number, number][] = [[i, j]];
                vis[i][j] = true;

                while (q.length) {
                    const [row, col] = q.shift()!;
                    currentFish += grid[row][col];

                    for (let d = 0; d < 4; d++) {
                        const nrow = row + delrow[d], ncol = col + delcol[d];
                        if (nrow >= 0 && nrow < m && ncol >= 0 && ncol < n && !vis[nrow][ncol] && grid[nrow][ncol] > 0) {
                            vis[nrow][ncol] = true;
                            q.push([nrow, ncol]);
                        }
                    }
                }
                ans = Math.max(currentFish, ans);
            }
        }
    }

    return ans;
}

```


['Depth-First Search', 'Breadth-First Search', 'Matrix', 'Python', 'C++', 'Java', 'TypeScript', 'Rust', 'Ruby', 'JavaScript']