`{"id":"f6e45534-0994-434c-8da6-6a352705e767","name":"Number Of Islands","description":"1. You are given a 2d array where 0's represent land and 1's represent water. \r\n Assume every cell is linked to it's north, east, west and south cell.\r\n2. You are required to find and count the number of islands.","inputFormat":"Input has been managed for you","outputFormat":"Number of islands","constraints":"None","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main()\n{\n int n, m;\n cin >> n;\n cin >> m;\n\n vector<vector<int>> arr;\n\n for (int i = 0; i < n; i++)\n {\n vector<int> ans;\n for (int j = 0; j < m; j++)\n {\n int res;\n cin >> res;\n ans.push_back(res);\n }\n arr.push_back(ans);\n }\n\n //write your code here\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int m = Integer.parseInt(br.readLine());\r\n int n = Integer.parseInt(br.readLine());\r\n int[][] arr = new int[m][n];\r\n\r\n for (int i = 0; i < arr.length; i++) {\r\n String parts = br.readLine();\r\n for (int j = 0; j < arr[0].length; j++) {\r\n arr[i][j] = Integer.parseInt(parts.split(\" \")[j]);\r\n }\r\n }\r\n\r\n // write your code here\r\n }\r\n\r\n}"},"python":{"code":"def main():\n n = int(input())\n m = int(input())\n arr = []\n \n for i in range(n):\n values = list(map(int , input().split(\" \")))\n arr.append(values)\n \n #write your code here\n\nmain()"}},"points":10,"difficulty":"easy","sampleInput":"8\r\n8\r\n0 0 1 1 1 1 1 1\r\n0 0 1 1 1 1 1 1\r\n1 1 1 1 1 1 1 0\r\n1 1 0 0 0 1 1 0\r\n1 1 1 1 0 1 1 0\r\n1 1 1 1 0 1 1 0\r\n1 1 1 1 1 1 1 0\r\n1 1 1 1 1 1 1 0","sampleOutput":"3","questionVideo":"https://www.youtube.com/embed/ErPZFxugYkI?start=0&end=139","hints":[],"associated":[{"id":"4d25d755-be50-4620-9aab-060c158ab173","name":"Which of the following can NOT be a base case? (Number Of Islands)","slug":"which-of-the-following-can-not-be-a-base-case-number-of-islands","type":4},{"id":"4e6a7b82-c266-4592-8ec8-b3f1368fe55a","name":"What is the time complexity? (Number Of Islands)","slug":"what-is-the-time-complexity-number-of-islands","type":4},{"id":"cf9234aa-859e-4db2-b686-16b7a5d97b1d","name":"How many islands can you find in this array?(Number Of Islands) 0 0 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 0 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 1 0 1 1 0 ","slug":"how-many-islands-can-you-find-in-this-array-number-of-islands-0-0-1-1-1-1-1-1-0-0-1-1-1-1-1-1-1-1-1-1-1-1-1-0-1-1-0-0-0-1-1-0-1-1-1-1-0-1-1-0-1-1-1-1-0-1-1-0-1-1-1-1-1-1-1-0-1-1-1-1-1-1-1-0","type":4},{"id":"f1a7d7a1-289e-4a60-97e8-e5bb3357b29d","name":"This question is an application of which of these problems? (Number Of Islands)","slug":"this-question-is-an-application-of-which-of-these-problems-number-of-islands","type":4}],"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":"02ab8fe3-cddd-43ad-b241-b8b1a2fcd52e","name":"Graphs For Beginners","slug":"graphs-for-beginners","type":0},{"id":"a3ee4aa5-e23c-4d8f-95c0-298a4e350a80","name":"Number Of Islands","slug":"number-of-islands","type":1}],"next":{"id":"32289697-b008-48b4-a28c-e7b4ce4303b2","name":"Number of Islands","type":3,"slug":"number-of-islands"},"prev":{"id":"777bb2a2-0a00-45bd-a2d0-c2f08b480cfd","name":"IS GRAPH CONNECTED","type":3,"slug":"is-graph-connected"}}}`

# Number Of Islands

1. You are given a 2d array where 0's represent land and 1's represent water. Assume every cell is linked to it's north, east, west and south cell. 2. You are required to find and count the number of islands.

`{"id":"f6e45534-0994-434c-8da6-6a352705e767","name":"Number Of Islands","description":"1. You are given a 2d array where 0's represent land and 1's represent water. \r\n Assume every cell is linked to it's north, east, west and south cell.\r\n2. You are required to find and count the number of islands.","inputFormat":"Input has been managed for you","outputFormat":"Number of islands","constraints":"None","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main()\n{\n int n, m;\n cin >> n;\n cin >> m;\n\n vector<vector<int>> arr;\n\n for (int i = 0; i < n; i++)\n {\n vector<int> ans;\n for (int j = 0; j < m; j++)\n {\n int res;\n cin >> res;\n ans.push_back(res);\n }\n arr.push_back(ans);\n }\n\n //write your code here\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int m = Integer.parseInt(br.readLine());\r\n int n = Integer.parseInt(br.readLine());\r\n int[][] arr = new int[m][n];\r\n\r\n for (int i = 0; i < arr.length; i++) {\r\n String parts = br.readLine();\r\n for (int j = 0; j < arr[0].length; j++) {\r\n arr[i][j] = Integer.parseInt(parts.split(\" \")[j]);\r\n }\r\n }\r\n\r\n // write your code here\r\n }\r\n\r\n}"},"python":{"code":"def main():\n n = int(input())\n m = int(input())\n arr = []\n \n for i in range(n):\n values = list(map(int , input().split(\" \")))\n arr.append(values)\n \n #write your code here\n\nmain()"}},"points":10,"difficulty":"easy","sampleInput":"8\r\n8\r\n0 0 1 1 1 1 1 1\r\n0 0 1 1 1 1 1 1\r\n1 1 1 1 1 1 1 0\r\n1 1 0 0 0 1 1 0\r\n1 1 1 1 0 1 1 0\r\n1 1 1 1 0 1 1 0\r\n1 1 1 1 1 1 1 0\r\n1 1 1 1 1 1 1 0","sampleOutput":"3","questionVideo":"https://www.youtube.com/embed/ErPZFxugYkI?start=0&end=139","hints":[],"associated":[{"id":"4d25d755-be50-4620-9aab-060c158ab173","name":"Which of the following can NOT be a base case? (Number Of Islands)","slug":"which-of-the-following-can-not-be-a-base-case-number-of-islands","type":4},{"id":"4e6a7b82-c266-4592-8ec8-b3f1368fe55a","name":"What is the time complexity? (Number Of Islands)","slug":"what-is-the-time-complexity-number-of-islands","type":4},{"id":"cf9234aa-859e-4db2-b686-16b7a5d97b1d","name":"How many islands can you find in this array?(Number Of Islands) 0 0 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 0 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 1 0 1 1 0 ","slug":"how-many-islands-can-you-find-in-this-array-number-of-islands-0-0-1-1-1-1-1-1-0-0-1-1-1-1-1-1-1-1-1-1-1-1-1-0-1-1-0-0-0-1-1-0-1-1-1-1-0-1-1-0-1-1-1-1-0-1-1-0-1-1-1-1-1-1-1-0-1-1-1-1-1-1-1-0","type":4},{"id":"f1a7d7a1-289e-4a60-97e8-e5bb3357b29d","name":"This question is an application of which of these problems? (Number Of Islands)","slug":"this-question-is-an-application-of-which-of-these-problems-number-of-islands","type":4}],"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":"02ab8fe3-cddd-43ad-b241-b8b1a2fcd52e","name":"Graphs For Beginners","slug":"graphs-for-beginners","type":0},{"id":"a3ee4aa5-e23c-4d8f-95c0-298a4e350a80","name":"Number Of Islands","slug":"number-of-islands","type":1}],"next":{"id":"32289697-b008-48b4-a28c-e7b4ce4303b2","name":"Number of Islands","type":3,"slug":"number-of-islands"},"prev":{"id":"777bb2a2-0a00-45bd-a2d0-c2f08b480cfd","name":"IS GRAPH CONNECTED","type":3,"slug":"is-graph-connected"}}}`

Editor

# Number Of Islands

easy

1. You are given a 2d array where 0's represent land and 1's represent water. Assume every cell is linked to it's north, east, west and south cell. 2. You are required to find and count the number of islands.

None

## Format

### Input

Input has been managed for you

### Output

Number of islands

## 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;}8 8 0 0 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 0 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 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`

Question Video

Discussions

Show Discussion

Related Resources