<p>You are given an <code>m x n</code> binary matrix <code>grid</code>. An island is a group of <code>1</code>&#39;s (representing land) connected <strong>4-directionally</strong> (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.</p>

<p>The <strong>area</strong> of an island is the number of cells with a value <code>1</code> in the island.</p>

<p>Return <em>the maximum <strong>area</strong> of an island in </em><code>grid</code>. If there is no island, return <code>0</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/01/maxarea1-grid.jpg" style="width: 500px; height: 310px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The answer is not 11, because the island must be connected 4-directionally.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [[0,0,0,0,0,0,0,0]]
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 50</code></li>
	<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

class Disjoint{
public:
    vector<int> parent;
    vector<int> size;

    Disjoint(int n) {
        parent.resize(n);
        size.resize(n);
        for(int i = 0; i < n; i++) {
            parent[i] = i;
            size[i] = 1;
        }
    }

    int Uparent(int u){
        if(parent[u] == u) return u;
        return parent[u] = Uparent(parent[u]);
    }

    void UnionBySize(int u, int v) {
        int u_par = Uparent(u);
        int v_par = Uparent(v);

        if(u_par == v_par) return ;
        if(size[u_par] >  size[v_par]){
            parent[v_par] = u_par;
            size[u_par] = size[u_par] + size[v_par];
        } else{
            parent[u_par] = v_par;
            size[v_par] = size[u_par] + size[v_par];
        }
    }
};

class Solution {
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();
        Disjoint ds(m * n);
        
        // BFS / DFS to connect all land cells
        vector<vector<int>> visited(m ,vector<int>(n, 0));
        
        for(int row = 0; row < m; row++) {
            for(int col = 0; col < n; col++) {
                if(grid[row][col] == 0) continue;
                int ele = row * n + col;
                if(visited[row][col] == 1) continue;

                queue<int> q;
                q.push(ele);
                visited[row][col] = 1;
                
                while (!q.empty()) {
                    int node = q.front();
                    q.pop();
                    
                    int curRow = node / n;
                    int curCol = node % n;
                    
                    int delrow[] = {-1, 0, +1, 0};
                    int delcol[] = {0, +1, 0, -1};
                    for(int i = 0; i < 4; i++) {
                        int nrow = curRow + delrow[i];
                        int ncol = curCol + delcol[i];
                        
                        if(nrow >= 0 && ncol >= 0 && nrow < m && ncol < n && 
                           grid[nrow][ncol] == 1 && visited[nrow][ncol] == 0) {
                            
                            visited[nrow][ncol] = 1;
                            int neighbor = nrow * n + ncol;
                            ds.UnionBySize(node, neighbor);
                            q.push(neighbor);
                        }
                    }
                }
            }
        }

        // Find the maximum size of any island by checking root parents
        int maxSize = 0;
        vector<int> rootSize(m * n, 0);
        
        for (int i = 0; i < m * n; i++) {
            if (grid[i / n][i % n] == 1) {
                int root = ds.Uparent(i);
                rootSize[root] = ds.size[root]; // Track the size of the root parent
                maxSize = max(maxSize, rootSize[root]); // Update max size
            }
        }
        
        return maxSize;
    }
};

32970