Count Land Components Enclosed Away From the Boundary

Quick Overview

Count Land Components Enclosed Away From the Boundary. In grid,0 means land and1 means water. An island is a maximal group of land cells joined horizontally or vertically.

Count Land Components Enclosed Away From the Boundary

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem In grid, 0 means land and 1 means water. An island is a maximal group of land cells joined horizontally or vertically. Count the islands completely surrounded by water on all four sides, so none of their cells touches the grid boundary. ```cpp class Solution { public: int closedIsland(vector<vector<int>>& grid); }; ``` ### Examples **Example 1:** ```text Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 1 Explanation: The center land cell is enclosed on all four sides. ``` **Example 2:** ```text Input: grid = [[1,0,1],[1,0,1],[1,1,1]] Output: 0 Explanation: The center land is connected to the top boundary, so its island is not closed. ``` ### Constraints - Each grid dimension is between 1 and 100. - Every entry is 0 or 1. ### Contract considerations Land is 0 here, not 1. Every boundary, rather than only corners, must be checked. ```hint Read the boundary condition Do diagonal land cells belong to the same island, and can land touching any outer edge qualify? ``` ### Boundary reminders Land is 0 here, not 1. Every boundary, rather than only corners, must be checked.

Overview: Count Land Components Enclosed Away From the Boundary. In grid,0 means land and1 means water. An island is a maximal group of land cells joined horizontally or vertically.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

In grid, 0 means land and 1 means water. An island is a maximal group of land cells joined horizontally or vertically. Count the islands completely surrounded by water on all four sides, so none of their cells touches the grid boundary.

class Solution {
public:
    int closedIsland(vector<vector<int>>& grid);
};

Examples

Example 1:

Input: grid = [[1,1,1],[1,0,1],[1,1,1]]
Output: 1
Explanation: The center land cell is enclosed on all four sides.

Example 2:

Input: grid = [[1,0,1],[1,0,1],[1,1,1]]
Output: 0
Explanation: The center land is connected to the top boundary, so its island is not closed.

Constraints

  • Each grid dimension is between 1 and 100.
  • Every entry is 0 or 1.

Contract considerations

Land is 0 here, not 1. Every boundary, rather than only corners, must be checked.

Boundary reminders

Land is 0 here, not 1. Every boundary, rather than only corners, must be checked.