package main
func (_ Solution) NumIslands(matrix [][]string) int {
count := 0
for row := 0; row < len(matrix); row ++ {
for col := 0; col < len(matrix[row]); col ++ {
if matrix[row][col] == "1" {
count ++
dfs(matrix, row, col)
}
}
}
return count
}
func dfs(matrix [][]string, row, col int){
if row < 0 || row >= len(matrix) || col < 0 || col >= len(matrix[row]) {
return
}
if matrix[row][col] == "0" {
return
}
matrix[row][col] = "0"
dfs(matrix, row-1, col)
dfs(matrix, row+1, col)
dfs(matrix, row, col-1)
dfs(matrix, row, col+1)
}
Graf-Dfs
Hali izoh yo'q. Birinchi bo'ling!