`{"id":"fc2701e9-dd5d-4b37-82de-8b96348c821e","name":"Number Of Enclaves","description":"You are given an m * n binary matrix grid, where 0 represents a sea cell and 1 represents a land cell. A move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid.\r\nReturn the number of land cells in grid for which we cannot walk off the boundary of the grid in any number of moves.","inputFormat":"First line contains two integers m and n.\r\nEach of next m line contains n integers 0 or 1.","outputFormat":"Print number of enclaves","constraints":"1 &lt;= m,n &lt;= 1000","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nint numEnclaves(vector<vector<int>>& arr){\n \n}\n\nint main(){\n int m,n;\n cin>>m>>n;\n \n vector<vector<int>>arr(m,vector<int>(n));\n \n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n int x;\n cin>>x;\n arr[i][j]=x;\n }\n }\n \n cout<<numEnclaves(arr);\n}\n"},"java":{"code":"import java.util.*;\nimport java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.lang.*;\n\nclass Main {\n\n public static void main(String[] args) throws NumberFormatException, IOException {\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n\n String[] st = br.readLine().split(\" \");\n int m = Integer.parseInt(st[0]);\n int n = Integer.parseInt(st[1]);\n\n int[][] arr = new int[m][n];\n\n for (int i = 0; i < m; i++) {\n st = br.readLine().split(\" \");\n for (int j = 0; j < n; j++) {\n arr[i][j] = Integer.parseInt(st[j]);\n }\n }\n\n System.out.println(numEnclaves(arr));\n\n }\n\n public static int numEnclaves(int[][] arr) {\n \n }\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4 4\r\n0 0 0 0\r\n1 0 1 0\r\n0 1 1 0\r\n0 0 0 0","sampleOutput":"3\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"7e07fddf-83bd-421e-848f-118f1f29541c","name":"Graphs For Intermediate","slug":"graphs-for-intermediate-493","type":0},{"id":"1310f58d-c780-4a3f-b719-4ab16494339a","name":"Number Of Enclaves","slug":"number-of-enclaves","type":1}],"next":{"id":"2949fb64-64d7-466c-8427-6b5fe7195d71","name":"Number of Enclaves MCQ","type":0,"slug":"number-of-enclaves-mcq"},"prev":{"id":"43749f6f-4e0c-4720-a6f4-7331717d7981","name":"Coloring a Border Medium MCQ","type":0,"slug":"coloring-a-border-medium-mcq"}}}`

# Number Of Enclaves

You are given an m * n binary matrix grid, where 0 represents a sea cell and 1 represents a land cell. A move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid. Return the number of land cells in grid for which we cannot walk off the boundary of the grid in any number of moves.

`{"id":"fc2701e9-dd5d-4b37-82de-8b96348c821e","name":"Number Of Enclaves","description":"You are given an m * n binary matrix grid, where 0 represents a sea cell and 1 represents a land cell. A move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid.\r\nReturn the number of land cells in grid for which we cannot walk off the boundary of the grid in any number of moves.","inputFormat":"First line contains two integers m and n.\r\nEach of next m line contains n integers 0 or 1.","outputFormat":"Print number of enclaves","constraints":"1 &lt;= m,n &lt;= 1000","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nint numEnclaves(vector<vector<int>>& arr){\n \n}\n\nint main(){\n int m,n;\n cin>>m>>n;\n \n vector<vector<int>>arr(m,vector<int>(n));\n \n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n int x;\n cin>>x;\n arr[i][j]=x;\n }\n }\n \n cout<<numEnclaves(arr);\n}\n"},"java":{"code":"import java.util.*;\nimport java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.lang.*;\n\nclass Main {\n\n public static void main(String[] args) throws NumberFormatException, IOException {\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n\n String[] st = br.readLine().split(\" \");\n int m = Integer.parseInt(st[0]);\n int n = Integer.parseInt(st[1]);\n\n int[][] arr = new int[m][n];\n\n for (int i = 0; i < m; i++) {\n st = br.readLine().split(\" \");\n for (int j = 0; j < n; j++) {\n arr[i][j] = Integer.parseInt(st[j]);\n }\n }\n\n System.out.println(numEnclaves(arr));\n\n }\n\n public static int numEnclaves(int[][] arr) {\n \n }\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4 4\r\n0 0 0 0\r\n1 0 1 0\r\n0 1 1 0\r\n0 0 0 0","sampleOutput":"3\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"7e07fddf-83bd-421e-848f-118f1f29541c","name":"Graphs For Intermediate","slug":"graphs-for-intermediate-493","type":0},{"id":"1310f58d-c780-4a3f-b719-4ab16494339a","name":"Number Of Enclaves","slug":"number-of-enclaves","type":1}],"next":{"id":"2949fb64-64d7-466c-8427-6b5fe7195d71","name":"Number of Enclaves MCQ","type":0,"slug":"number-of-enclaves-mcq"},"prev":{"id":"43749f6f-4e0c-4720-a6f4-7331717d7981","name":"Coloring a Border Medium MCQ","type":0,"slug":"coloring-a-border-medium-mcq"}}}`

Editor

# Number Of Enclaves

easy

You are given an m * n binary matrix grid, where 0 represents a sea cell and 1 represents a land cell. A move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid. Return the number of land cells in grid for which we cannot walk off the boundary of the grid in any number of moves.

1 <= m,n <= 1000

## Format

### Input

First line contains two integers m and n. Each of next m line contains n integers 0 or 1.

### Output

Print number of enclaves

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}4 4 0 0 0 0 1 0 1 0 0 1 1 0 0 0 0 0```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}3 ```

Discussions

Show Discussion

Related Resources