- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find the number of distinct islands in a 2D matrix in Python

Suppose we have a binary matrix. We have to count the number of islands in it. An island is place that is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. We can assume that all four edges of the grid are all surrounded by water.

Suppose the grid is like −

1 | 1 | 0 | 0 | 0 |

1 | 1 | 0 | 0 | 0 |

0 | 0 | 1 | 0 | 0 |

0 | 0 | 0 | 1 | 1 |

There are three islands.

To solve this, we will follow these steps −

There will be two methods, one will be used to count number of islands called numIslands() and makeWater(). The makeWater() will be like −

if number of rows in the grid is 0, then return 0

n = row count and m := column count, and ans := 0

for i in range 0 to n – 1

for j in range 0 to m

if grid[i, j] = 1, then ans := ans + 1

makeWater(i, j, n, m, grid)

the makeWater() will take the indices i, j, row and col count n and m, and grid

if i <0 or j < 0 or i >= n or j >= m, then return from this method

if grid[i, j] = 0, then return otherwise make grid[i, j] := 0

call makeWater(i + 1, j, n, m, grid)

call makeWater(i, j + 1, n, m, grid)

## Example

Let us see the following implementation to get better understanding −

class Solution(object): def numIslands(self, grid): """ :type grid: List[List[str]] :rtype: int """ if len(grid) == 0: return 0 n= len(grid) m = len(grid[0]) ans = 0 for i in range(n): for j in range(m): if grid[i][j] == "1": ans+=1 self.make_water(i,j,n,m,grid) return ans def make_water(self,i,j,n,m,grid): if i<0 or j<0 or i>=n or j>=m: return if grid[i][j] == "0": return else: grid[i][j]="0" self.make_water(i+1,j,n,m,grid) self.make_water(i,j+1,n,m,grid) self.make_water(i-1,j,n,m,grid) self.make_water(i,j-1,n,m,grid)

## Input

[["1","1","0","0","0"], ["1","1","0","0","0"], ["0","0","1","0","0"], ["0","0","0","1","1"]]

## Output

3

- Related Questions & Answers
- Number of Distinct Islands in C++
- Number of Distinct Islands II in C++
- Program to count number of surrounded islands in the matrix in python
- Program to count number of islands in a given matrix in Python
- Number of Islands in Python
- Find the number of islands Using DFS in C++
- Program to find number of distinct island shapes from a given matrix in Python
- How to print number of islands in a given matrix using C#?
- Find the number of Islands Using Disjoint Set in C++
- Find distinct elements common to all rows of a matrix in Python
- Search a 2D Matrix II in Python
- Program to find number of distinct subsequences in Python
- Number of Closed Islands in C++
- Program to find number of islands, from where we cannot leave in Python
- Search a 2D Matrix in C++