 2 Approaches 😈😈|| Beats 💯% with proof 📜 || Easy DFS 🌲 || Recursion 🔄
Approach - 1Complexity
Time complexity: O(M∗N)

Space complexity: O(M∗N)
CodeApproach - 2Complexity
Time complexity: O(M∗N)

Space complexity: O(1)
Code
Please
![image.png](https://assets.leetcode.com/users/images/29a70741-25ce-4cb4-be16-3b67e1c682bb_1738045643.978566.png)
```
Plzzz... Upvote!!
```

# Approach - 1
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity: $$O(M*N)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(M*N)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
    int m,n;
    int solve(int i,int j,vector<vector<int>>&grid,vector<vector<bool>>&vis){
        if(i<0 || i>=m || j<0 || j>=n || vis[i][j] || !grid[i][j]){
            return 0;
        }
        int temp=grid[i][j];
        vis[i][j]=1;
        temp+=solve(i+1,j,grid,vis);
        temp+=solve(i-1,j,grid,vis);
        temp+=solve(i,j+1,grid,vis);
        temp+=solve(i,j-1,grid,vis);
        return temp;
    }
    int findMaxFish(vector<vector<int>>& grid) {
        m=grid.size(),n=grid[0].size();
        int ans=0;
        vector<vector<bool>>vis(m,vector<bool>(n,0));
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(grid[i][j] && !vis[i][j]){
                    ans=max(ans,solve(i,j,grid,vis));
                }
            }
        }
        return ans;
    }
};
```
```java []
class Solution {
    int m, n;
    public int solve(int i, int j, int[][] grid, boolean[][] vis) {
        if (i < 0 || i >= m || j < 0 || j >= n || vis[i][j] || grid[i][j] == 0) return 0;
        int temp = grid[i][j];
        vis[i][j] = true;
        temp += solve(i + 1, j, grid, vis);
        temp += solve(i - 1, j, grid, vis);
        temp += solve(i, j + 1, grid, vis);
        temp += solve(i, j - 1, grid, vis);
        return temp;
    }
    public int findMaxFish(int[][] grid) {
        m = grid.length;
        n = grid[0].length;
        int ans = 0;
        boolean[][] vis = new boolean[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] != 0 && !vis[i][j]) {
                    ans = Math.max(ans, solve(i, j, grid, vis));
                }
            }
        }
        return ans;
    }
}
```
```javascript []
var m, n;
var solve = function(i, j, grid, vis) {
    if (i < 0 || i >= m || j < 0 || j >= n || vis[i][j] || grid[i][j] === 0) return 0;
    let temp = grid[i][j];
    vis[i][j] = true;
    temp += solve(i + 1, j, grid, vis);
    temp += solve(i - 1, j, grid, vis);
    temp += solve(i, j + 1, grid, vis);
    temp += solve(i, j - 1, grid, vis);
    return temp;
};
var findMaxFish = function(grid) {
    m = grid.length;
    n = grid[0].length;
    let ans = 0;
    let vis = Array.from({ length: m }, () => Array(n).fill(false));
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (grid[i][j] !== 0 && !vis[i][j]) {
                ans = Math.max(ans, solve(i, j, grid, vis));
            }
        }
    }
    return ans;
};
```
```python []
class Solution:
    def solve(self, i, j, grid, vis):
        if i < 0 or i >= self.m or j < 0 or j >= self.n or vis[i][j] or grid[i][j] == 0:
            return 0
        temp = grid[i][j]
        vis[i][j] = True
        temp += self.solve(i + 1, j, grid, vis)
        temp += self.solve(i - 1, j, grid, vis)
        temp += self.solve(i, j + 1, grid, vis)
        temp += self.solve(i, j - 1, grid, vis)
        return temp

    def findMaxFish(self, grid):
        self.m, self.n = len(grid), len(grid[0])
        ans = 0
        vis = [[False] * self.n for _ in range(self.m)]
        for i in range(self.m):
            for j in range(self.n):
                if grid[i][j] != 0 and not vis[i][j]:
                    ans = max(ans, self.solve(i, j, grid, vis))
        return ans
```

---



# Approach - 2
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity: $$O(M*N)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(1)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
    int m,n;
    int solve(int i,int j,vector<vector<int>>&grid){
        if(i<0 || i>=m || j<0 || j>=n || !grid[i][j]){
            return 0;
        }
        int temp=grid[i][j];
        grid[i][j]=0;
        temp+=solve(i+1,j,grid);
        temp+=solve(i-1,j,grid);
        temp+=solve(i,j+1,grid);
        temp+=solve(i,j-1,grid);
        return temp;
    }
    int findMaxFish(vector<vector<int>>& grid) {
        m=grid.size(),n=grid[0].size();
        int ans=0;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(grid[i][j]){
                    ans=max(ans,solve(i,j,grid));
                }
            }
        }
        return ans;
    }
};
```
```java []
class Solution {
    int m, n;
    public int solve(int i, int j, int[][] grid) {
        if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) return 0;
        int temp = grid[i][j];
        grid[i][j] = 0;
        temp += solve(i + 1, j, grid);
        temp += solve(i - 1, j, grid);
        temp += solve(i, j + 1, grid);
        temp += solve(i, j - 1, grid);
        return temp;
    }
    public int findMaxFish(int[][] grid) {
        m = grid.length;
        n = grid[0].length;
        int ans = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] != 0) {
                    ans = Math.max(ans, solve(i, j, grid));
                }
            }
        }
        return ans;
    }
}
```
```javascript []
var m, n;
var solve = function(i, j, grid) {
    if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] === 0) return 0;
    let temp = grid[i][j];
    grid[i][j] = 0;
    temp += solve(i + 1, j, grid);
    temp += solve(i - 1, j, grid);
    temp += solve(i, j + 1, grid);
    temp += solve(i, j - 1, grid);
    return temp;
};
var findMaxFish = function(grid) {
    m = grid.length;
    n = grid[0].length;
    let ans = 0;
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (grid[i][j] !== 0) {
                ans = Math.max(ans, solve(i, j, grid));
            }
        }
    }
    return ans;
};
```
```python []
class Solution:
    def solve(self, i, j, grid):
        if i < 0 or i >= self.m or j < 0 or j >= self.n or grid[i][j] == 0:
            return 0
        temp = grid[i][j]
        grid[i][j] = 0
        temp += self.solve(i + 1, j, grid)
        temp += self.solve(i - 1, j, grid)
        temp += self.solve(i, j + 1, grid)
        temp += self.solve(i, j - 1, grid)
        return temp

    def findMaxFish(self, grid):
        self.m, self.n = len(grid), len(grid[0])
        ans = 0
        for i in range(self.m):
            for j in range(self.n):
                if grid[i][j] != 0:
                    ans = max(ans, self.solve(i, j, grid))
        return ans
```

---

> Please Upvote !! 
![image.png](https://assets.leetcode.com/users/images/ab348137-81c9-4ef1-a186-6b2ae541bea0_1738045715.7929811.png)

['Array', 'Math', 'Depth-First Search', 'Recursion', 'Matrix', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']