Max Number of Fish in a Grid
You are given a 0-indexed 2D matrix grid of size m x n, where each cell contains a non-negative integer representing the number of fish at that cell.
A fisher can start at any water cell (a cell with fish > 0) and can move up, down, left, or right to adjacent water cells. The fisher collects all fish in every cell visited.
Return the maximum number of fish that can be collected in one fishing trip, or 0 if no water cells exist.
Note: The fisher can only move between cells with fish > 0 (water cells). Land cells (fish = 0) block movement.
Examples
Input: [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]
Output: 7
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]
Output: 1
Hints
Identify all water cells (cells with fish > 0) and treat them as nodes in a graph where edges exist between adjacent water cells.
The problem reduces to finding the connected component with the maximum sum of fish in the graph of water cells.
Use Depth-First Search (DFS) or Breadth-First Search (BFS) to explore each connected component of water cells, calculate the total fish in each, and keep track of the maximum.
Related Problems
Max Number of Fish in a Grid
You are given a 0-indexed 2D matrix `grid` of size `m x n`, where each cell contains a non-negative integer representing the number of fish at that cell.