DSU Solution || Path Compression and Ackermann Concept Explained || Time And Space Explained ||
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

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

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

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

# Code
```cpp []
//Data Set Union to check connected Components 
class DSU {
public:
    vector<int> parent;//To keep a track of which component the element is 
    vector<int> rank;//To create a dynamically changing graph 
    DSU(int n) {//Initializing the DSU
        parent.resize(n);
        rank.resize(n);
        for (int i = 0; i < n; i++) {
            parent[i] = i;
            rank[i] = 0;
        }
    }
    int find_parent(int n) {//Using Find Parent we can acess whether a elemnt is from certain component or not 
        if (parent[n] == n) {
            return n;
        } else {
            return parent[n] = find_parent(parent[n]);//Path Compression 
        }
    }
    void merge(int a, int b) {//Merging By Rank
        int u = find_parent(a);
        int v = find_parent(b);
        if (u == v)
            return;
        if (rank[u] > rank[v]) {
            parent[v] = u;
        } else if (rank[v] > rank[u]) {
            parent[u] = v;
        } else {
            parent[u] = v;
            rank[v]++;
        }
    }
};
class Solution {
public:
    //Direction vector to easily navigate the alternate neighbours 
    vector<int> x = {1, -1, 0, 0};
    vector<int> y = {0, 0, 1, -1};
    int findMaxFish(vector<vector<int>>& grid) {
        if (grid.empty() || grid[0].empty()){//Edge Case
            return 0;
        }
        int n = grid.size();
        int m = grid[0].size();
        DSU dsu(n * m);//Inittializing the DSU Object
        bool flag = true;//To check if there exists water cell
        for (int i = 0; i < n; i++) {//O(Row*Col)
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == 0) {
                    continue;
                } else {
                    flag = false;
                    int a = i * m + j;
                    for (int k = 0; k < 4; k++) {//Traversing the neighbor elemnts 
                        if (i + x[k] >= 0 && i + x[k] < n && j + y[k] >= 0 &&
                            j + y[k] < m && grid[i + x[k]][j + y[k]] != 0) {
                            int b = (i + x[k]) * m + (j + y[k]);
                            dsu.merge(a, b);//merging who are part of dame componets 
                        }
                    }
                }
            }
        }
        if (flag) {//When no water cell is present 
            return 0;
        }
        unordered_map<int, int> mp;//To track all elemts and find therir sum 
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int p = dsu.find_parent(i * m + j);
                mp[p] += grid[i][j];
            }
        }
        int ans = INT_MIN;
        for (auto i : mp) {
            ans = max(i.second, ans);
        }
        return ans;
        //Time Complexity :O(Row*Col*4)+O(αn)≈O(Row*col*4)
        //where α is Ackermann Constant 
        //Space Complexity :O(M*N)

        //Other Ideas : Can be used using a normal Bfs or Dfs Search ....
    }
};
```
['C++']