In Python, you can utilize the Depth-First Search (DFS) method, which is particularly effective for problems like this one. Begin at a cell, and if it represents an island, perform DFS to label all neighboring islands, then increase the counter. Below is a straightforward implementation: def dfs ( grid , i , j ) : if i < 0 or j < 0 or i >= len ( grid ) or j >= len ( grid [ 0 ] ) or grid [ i ] [ j ] != '1' : return grid [ i ] [ j ] = '#' dfs ( grid , i + 1 , j ) dfs ( grid , i - 1 , j ) dfs ( grid , i , j + 1 ) dfs ( grid , i , j - 1 ) def numIslands ( grid ) : if not grid : return 0 count = 0 for i in range ( len ( grid ) ) : for j in range ( len ( grid [ 0 ] ) ) : if grid [ i ] [ j ] == '1' : dfs ( grid , i , j ) count += 1 return count In this code, the numIslands function receives the grid as input and returns the total number of islands. Within this function, for every unvisited cell containing a '1', we increase the count and invoke the dfs function to mark all cells in the current island as visited. The dfs function marks the current cell as visited and recursively calls itself for all neighboring cells.