❄️Simple Recursion to solve easily- C++ || beats 100% 🔥
IntuitionUsing Recursion to traverse each cell where there is fish and counting them. After Finishing I realised it's actually called DFS for searching like thi
# Intuition
Using Recursion to traverse each cell where there is fish and counting them. After Finishing I realised it's actually called DFS for searching like this. So this is a simple recursive DFS code.

# Approach
Traverse Each element from top right to botton left
```
for(int i=0;i<m;i++){
    for(int j=0;j<n;j++){
        rec(i,j,cnt,v);
        x=max(cnt,x);
        cnt=0;
    }
}
```

The recursive call "rec" calls each element and the recursion function moves to each cell where there is fish and also assign v[i][j]=0, which is important, so that the already searched cell won't be searched again!!

> Think again does the already searched element need to be searched?

`Recursive Code`
```
    void rec(int i,int j,int& cnt,vector<vector<int>>& v){
        cnt+=v[i][j];
        if(v[i][j]==0) return;
        v[i][j]=0;
        if(i>0 && v[i-1][j]>0 && j>=0 && j<n) rec(i-1,j,cnt,v);
        if(j>0 && v[i][j-1]>0 && i>=0 && i<m) rec(i,j-1,cnt,v);
        if(j<n-1 && v[i][j+1] && i>=0 && i<m) rec(i,j+1,cnt,v);
        if(i<m-1 && v[i+1][j] && j>=0 && j<n) rec(i+1,j,cnt,v);
        else{
            return;
        }
    }
```

And of course to find the largest among the count taken we have to print the max. So...
`Line: 26`
```
x=max(cnt,x);
cnt=0;
```


# Complexity
- Time complexity:
$$O(n*m)$$

- Space complexity:
$$O(n*m)$$

# Code
```cpp []
class Solution {
public:
    int m,n;
    void rec(int i,int j,int& cnt,vector<vector<int>>& v){
        cnt+=v[i][j];
        if(v[i][j]==0) return;
        v[i][j]=0;
        if(i>0 && v[i-1][j]>0 && j>=0 && j<n) rec(i-1,j,cnt,v);
        if(j>0 && v[i][j-1]>0 && i>=0 && i<m) rec(i,j-1,cnt,v);
        if(j<n-1 && v[i][j+1] && i>=0 && i<m) rec(i,j+1,cnt,v);
        if(i<m-1 && v[i+1][j] && j>=0 && j<n) rec(i+1,j,cnt,v);
        else{
            return;
        }
    }


    int findMaxFish(vector<vector<int>>& v) {
        m=v.size();
        n=v[0].size();
        int cnt=0;
        int x=0;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                rec(i,j,cnt,v);
                x=max(cnt,x);
                cnt=0;
            }
        }
        return x;
    }
};
```
['Array', 'Depth-First Search', 'Matrix', 'C++']