C++ Easy and Clean DFS Solution, Explained, 0 ms Beats 100%
Algorithm Explanation:This algorithm uses Depth-First Search (DFS) to explore connected regions of non-zero fish values in a grid and finds the largest "lake" o
# Algorithm Explanation:
This algorithm uses Depth-First Search (DFS) to explore connected regions of non-zero fish values in a grid and finds the largest "lake" of fish.

##### Initialization:

We iterate over the entire grid to find unvisited cells with fish (grid[i][j] > 0).
We use a `visited` matrix to track which cells have already been explored.
When we find a cell with fish, we start a DFS to explore all connected cells (up, down, left, right).
We mark the cell as visited to avoid counting it multiple times.
We recursively call DFS on neighboring cells and sum up all fish in the connected region.
The DFS function returns the total fish count for that connected region.
Each DFS call gives us the total fish in a connected component (lake).
We update the maximum fish count found so far.
Time Complexity:

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

- Space complexity: O(n*m)

# Code
```cpp []
class Solution {
public:
    vector<int> dir_x = {-1,1,0,0}, dir_y = {0,0,-1,1};

    bool isValid(vector<vector<int>>& grid, int i, int j) {
        return i < grid.size() && i >= 0 && j < grid[0].size() && j >= 0;
    }

    int dfs(vector<vector<int>>& grid, vector<vector<int>>& visited, int i, int j) {
        if (isValid(grid, i, j) == false || grid[i][j] == 0 || visited[i][j] == 1) return 0;
        
        visited[i][j] = 1;
        int sum = grid[i][j];

        for (int dir = 0; dir < 4; dir++) {
            int curr_i = i + dir_x[dir], curr_j = j + dir_y[dir];
            sum += dfs(grid, visited, curr_i, curr_j);
        }

        return sum;
    }
    int findMaxFish(vector<vector<int>>& grid) {
        int res = 0;
        vector<vector<int>> visited(grid.size(), vector<int>(grid[0].size(), 0));

        for (int i = 0; i < grid.size(); i++) {
            for (int j = 0; j < grid[0].size(); j++) {
                if (visited[i][j] == 0 && grid[i][j] > 0) {
                    res = max(res, dfs(grid, visited, i, j));
                }
            }
        }
        
        return res;
    }
};
```

## Like it? please upvote!
['C++']