1001. Number of Enclaves

EasyGraphsGridDFSGraph

Given a grid of 1s (land) and 0s (water), return the number of land cells from which you cannot walk off the boundary of the grid in any number of 4-directional moves (land cells not connected to the border). The input is JSON {grid}.

Input: JSON {grid}.

Output: Integer — the count of enclosed land cells.

Examples

Example 1
Input: {"grid":[[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]}
Output: 3
Explanation: Three cells cannot reach the border.
Example 2
Input: {"grid":[[1]]}
Output: 0
Explanation: Border cell escapes.
Example 3
Input: {"grid":[[0,0],[0,0]]}
Output: 0
Explanation: No land.

Constraints

Asked by

GoogleBloombergMicrosoftAmazonMeta
Solve this problem in the editor →