def count_islands(matrix):
islands = 0
for row in range(len(matrix)):
for column in range(len(matrix[row])):
if matrix[row][column] == '1':
islands += 1
remove_island(matrix, row, column)
return islands
def remove_island(matrix, row, column):
if matrix[row][column] == '1':
matrix[row][column] = '0'
if row > 0:
remove_island(matrix, row - 1, column)
if row < len(matrix) - 1:
remove_island(matrix, row + 1, column)
if column > 0:
remove_island(matrix, row, column - 1)
if column < len(matrix[0]) - 1:
remove_island(matrix, row, column + 1)
test_case_0 = [
['1','1','0','0','0'],
['0','1','0','0','1'],
['1','0','0','1','1'],
['1','0','0','0','0'],
['1','0','1','0','1']
]
test_case_1 = [
['1','1','0','1','0'],
['1','1','0','0','1'],
['1','0','0','1','1'],
['1','0','0','0','0'],
['1','0','1','1','1']
]
print(count_islands(test_case_0))
print(count_islands(test_case_1))