<p>You are given an <code>m x n</code> matrix <code>board</code> containing <strong>letters</strong> <code>&#39;X&#39;</code> and <code>&#39;O&#39;</code>, <strong>capture regions</strong> that are <strong>surrounded</strong>:</p>

<ul>
	<li><strong>Connect</strong>: A cell is connected to adjacent cells horizontally or vertically.</li>
	<li><strong>Region</strong>: To form a region <strong>connect every</strong> <code>&#39;O&#39;</code> cell.</li>
	<li><strong>Surround</strong>: The region is surrounded with <code>&#39;X&#39;</code> cells if you can <strong>connect the region </strong>with <code>&#39;X&#39;</code> cells and none of the region cells are on the edge of the <code>board</code>.</li>
</ul>

<p>A <strong>surrounded region is captured</strong> by replacing all <code>&#39;O&#39;</code>s with <code>&#39;X&#39;</code>s in the input matrix <code>board</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">board = [[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;O&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;X&quot;,&quot;O&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;X&quot;,&quot;X&quot;]]</span></p>

<p><strong>Output:</strong> <span class="example-io">[[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;X&quot;,&quot;X&quot;,&quot;X&quot;],[&quot;X&quot;,&quot;O&quot;,&quot;X&quot;,&quot;X&quot;]]</span></p>

<p><strong>Explanation:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/xogrid.jpg" style="width: 367px; height: 158px;" />
<p>In the above diagram, the bottom region is not captured because it is on the edge of the board and cannot be surrounded.</p>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">board = [[&quot;X&quot;]]</span></p>

<p><strong>Output:</strong> <span class="example-io">[[&quot;X&quot;]]</span></p>
</div>

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

<ul>
	<li><code>m == board.length</code></li>
	<li><code>n == board[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 200</code></li>
	<li><code>board[i][j]</code> is <code>&#39;X&#39;</code> or <code>&#39;O&#39;</code>.</li>
</ul>

class Solution {
public:
    unordered_map<int, int> id;
    unordered_map<int, bool> border;
    unordered_map<int, int> sz;

    int find(int p){
        int root = p;
        while(id[root] != root){
            root = id[root];
        }
        while(p != root){
            int next = id[p];
            id[p] = root;
            p = next;
        }
        return root;
    }
    void unify(int p, int q){
        int rootP = find(p), rootQ = find(q);

        if(rootP == rootQ) return;

        if(sz[rootP] > sz[rootQ]){
            sz[rootP] += sz[rootQ];
            id[rootQ] = rootP;
        }
        else{
            sz[rootQ] += sz[rootP];
            id[rootP] = rootQ;
        }
        if(border[rootP] || border[rootQ]) border[rootP] = border[rootQ] = true;
    }

    bool capturable(int p){
        int rootP = find(p);
        return !border[rootP];
    }
    void solve(vector<vector<char>>& board) {
        // assign a set for every element initially
        // how do we figure out whether a region can be captured?
        // okay, got it, so if even a single 'O' in the region lies on the boundary
        // then it cannot be captured
        int m = board.size(), n = board[0].size();

        for(int r = 0; r < m; r++){
            for(int c = 0; c < n; c++){
                int off = r*n + c;
                id[off] = off;
                sz[off] = 1;
                if(board[r][c] == 'O' && (c == 0 || r == 0 || r == m - 1 || c == n - 1)) border[off] = true;
            }   
        }

        for(int r = 0; r < m; r++){
            for(int c = 0; c < n; c++){
                if(c != n - 1 && board[r][c + 1] == board[r][c]) unify(r*n + c + 1, r*n + c);
                if(r != m - 1 && board[r + 1][c] == board[r][c]) unify((r + 1)*n + c, r*n + c);
            }
        }

        for(int r = 0; r < m; r++){
            for(int c = 0; c < n; c++){
                if(capturable(r*n + c)) board[r][c] = 'X';
            }
        }
    }
};
22628