<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>

#include <vector>
#include <queue>
using namespace std;

class Solution {
public:

    // Function to perform BFS traversal from a given starting cell (i, j)
    void bfs(vector<vector<int>>& grid, int i, int j) {
        // Directions for moving in the grid: down, right, up, left
        vector<vector<int>> directions = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
        queue<pair<int, int>> Q;
        
        // Start BFS from the given cell and mark it as visited
        Q.push({i, j});
        grid[i][j] = 0;  // Mark the starting cell as visited

        // Process the queue until it's empty
        while (!Q.empty()) {
            // Get the front element of the queue
            auto [x, y] = Q.front();  
            Q.pop();

            // Explore all possible directions from the current cell
            for (const auto& dir : directions) {
                int nx = x + dir[0];  // New row index
                int ny = y + dir[1];  // New column index

                // Check if the new cell is within bounds and is part of the region (i.e., has a value of 1)
                if (nx >= 0 && nx < grid.size() && ny >= 0 && ny < grid[0].size() && grid[nx][ny] == 1) {
                    // Add the new cell to the queue and mark it as visited
                    Q.push({nx, ny});  
                    grid[nx][ny] = 0;  // Mark the new cell as visited
                }
            }
        }
    }

    // Function to count the number of enclaves in the grid
    int numEnclaves(vector<vector<int>>& grid) {
        int rows = grid.size();             // Number of rows in the grid
        int columns = grid[0].size();       // Number of columns in the grid
    
        // Perform BFS from all boundary cells to mark all regions connected to the boundary
        // Top and bottom rows
        for (int row = 0; row < rows; row++) {
            if (grid[row][0] == 1) {         // Check left boundary (first column)
                bfs(grid, row, 0);           // Perform BFS from the left boundary cell
            }
            if (grid[row][columns - 1] == 1) { // Check right boundary (last column)
                bfs(grid, row, columns - 1); // Perform BFS from the right boundary cell
            }
        }

        // Left and right columns
        for (int column = 0; column < columns; column++) {
            if (grid[0][column] == 1) {        // Check top boundary (first row)
                bfs(grid, 0, column);         // Perform BFS from the top boundary cell
            }
            if (grid[rows - 1][column] == 1) { // Check bottom boundary (last row)
                bfs(grid, rows - 1, column);  // Perform BFS from the bottom boundary cell
            }
        }

        // Count the number of remaining cells with value 1 (enclaves)
        int count = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                if (grid[i][j] == 1) {         // Check if the cell is an enclave (value 1)
                    count++;                  // Increment the enclave count
                }
            }
        }

        return count; // Return the total number of enclaves
    }
};

31727