Simple py,JAVA code explained in detail!! BFS!!
Probelm statement
Given m*n matrix named grid
If grid[i][j]>0 then we have a fish in it.
we have to maximise the collection of fish such that we can either

Tak
## Probelm statement

- Given `m*n` matrix named `grid`
- If `grid[i][j]>0` then we have a *fish* in it.
- we have to maximise the collection of fish such that we can either
    - Take the fish in `grid[i][j]`.
    -  Move to water cell which is `grid[i][j]=0`.
---
# Approach:BFS
<!-- Describe your approach to solving the problem. -->
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
- This question is equivalent of finding ***maximum area of given islands***.
- `count` is used to record the maximum fishes in a given water surrounded region.
- Have applied  `BFS` to calculate `fish`.
# Complexity
- Time complexity:$O(n^2)$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:$O(n^2)$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```Python3 []
#12:31
class Solution:
    def findMaxFish(self, grid: List[List[int]]) -> int:
        row,col=len(grid),len(grid[0])
        count=0
        visited=[[False]*col for _ in range(row)]
        directions=[(0,1),(1,0),(-1,0),(0,-1)]
        def BFS(r,c):
            visited[r][c]=True
            q=deque([(r,c)])
            fish=0
            while q:
                x,y=q.popleft()
                fish+=grid[x][y]
                for dx,dy in directions:
                    nx,ny=x+dx,y+dy
                    if nx<0 or ny<0 or nx>=row or ny>=col or visited[nx][ny]:
                        continue
                    else:
                        if grid[nx][ny]>0:
                            #fish+=grid[nx][ny]
                            q.append((nx,ny))
                            visited[nx][ny]=True
            #print(i,j,fish)
            #print('------------------------')
            return fish
        for i in range(row):
            for j in range(col):
                if grid[i][j]>0 and not visited[i][j]:
                    count=max(count,BFS(i,j))
                    #print(i,j,count)
        return count
```
```java []
class Solution {
    int row,col;
    boolean visited[][];
    int directions[][];
    int grid[][];
    private int BFS(int r,int c){
        visited[r][c]=true;
        int fish=0;
        Queue<int[]> q=new LinkedList<>();
        q.add(new int[]{r,c});
        while(!q.isEmpty()){
            int[] n=q.poll();
            int x=n[0];
            int y=n[1];
            fish+=grid[x][y];
            for(int pt=0;pt<directions.length;pt++){
                int nx=x+directions[pt][0];
                int ny=y+directions[pt][1];
                if(nx<0||ny<0||nx>=row||ny>=col||visited[nx][ny]){
                    continue;
                }
                else{
                    if(grid[nx][ny]>0){
                        visited[nx][ny]=true;
                        q.add(new int[]{nx,ny});
                    }
                }
            }
        }
        return fish;
    }
    public int findMaxFish(int[][] grid) {
        this.grid=grid;
        row=grid.length;
        col=grid[0].length;
        visited=new boolean[row][col];
        directions=new int[][]{{0,1},{1,0},{-1,0},{0,-1}};
        int count=0;
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(grid[i][j]>0 && !visited[i][j]){
                    count=Math.max(count,BFS(i,j));
                }
            }
        }
        return count;
    }
}
```
['Breadth-First Search', 'Matrix', 'Java', 'Python3']