for r inrange(s_r): for c inrange(s_c): if grid[r][c] == "1": island_num += 1 grid[r][c] = "0" que = [(r,c)] while que: row,col = que.pop() for x,y in [(row-1,col),(row+1,col),(row,col-1),(row,col+1)]: if0<=x<s_r and0<=y<s_c and grid[x][y] == "1": que.append((x,y)) grid[x][y] = "0"