<p>You are given an <code>m x n</code> binary matrix <code>grid</code>, where <code>0</code> represents a sea cell and <code>1</code> represents a land cell.</p>

<p>A <strong>move</strong> consists of walking from one land cell to another adjacent (<strong>4-directionally</strong>) land cell or walking off the boundary of the <code>grid</code>.</p>

<p>Return <em>the number of land cells in</em> <code>grid</code> <em>for which we cannot walk off the boundary of the grid in any number of <strong>moves</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/enclaves1.jpg" style="width: 333px; height: 333px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> There are three 1s that are enclosed by 0s, and one 1 that is not enclosed because its on the boundary.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/enclaves2.jpg" style="width: 333px; height: 333px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All 1s are either on the boundary or can reach the boundary.
</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;= 500</code></li>
	<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

class Solution {
    struct UnionFind {
        UnionFind(size_t N) : parents(N) {
            std::iota(parents.begin(), parents.end(), 0);
        }
        
        int find(int value) {
            if (parents[value] == value)
                return value;
            
            parents[value] = find(parents[value]);
            
            return parents[value];
        }
        
        void merge(int f, int s) {
            const auto root1 = find(f);
            const auto root2 = find(s);
            
            if (root1 != root2)
                parents[root2] = root1;
        }
        
        std::vector<int> parents;
    };
    
public:
    int numEnclaves(vector<vector<int>>& grid) {
        int result = 0;
        
        const auto N = grid.size();
        const auto M = grid[0].size();
        
        UnionFind uf(N * M);
        
        auto add = [&grid, &uf, N, M](int y1, int x1, int y2, int x2) {
            if (0 <= y2 && y2 < N && 0 <= x2 && x2 < M && grid[y2][x2] == 1)
                uf.merge(y1 * M + x1, y2 * M + x2);
        };
        
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < M; ++j) {
                if (grid[i][j] == 1) {
                    add(i, j, i + 1, j);
                    add(i, j, i - 1, j);
                    add(i, j, i, j + 1);
                    add(i, j, i, j - 1);
                }
            }
        }
        
        std::unordered_set<int> roots;
        
        for (int i = 0; i < N; ++i)
            for (int j = 0; j < M; ++j)
                if (grid[i][j] == 1 && (i == 0 || i == N - 1 || j == 0 || j == M - 1))
                    roots.insert(uf.find(i * M + j));

        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < M; ++j) {
                if (grid[i][j] == 1) {
                    const auto root = uf.find(i * M + j);
                    if (roots.end() == roots.find(root))
                        ++result;
                }
            }
        }
        
        return result;
    }
};
29417