1defbomberMan(n, grid):2# Write your code here3if n ==1:return grid
45# all cells will be filled with bombs6if n %2==0:return["O"*len(grid[0])for _ inrange(len(grid))]78# alternate states9 n //=21011for q inrange((n +1)%2+1):12 newGrid =[["O"for _ inrange(len(grid[0]))]for _ inrange(len(grid))]1314# function for detonation15defdetonate(x, y):16if0<= x <len(grid)and0<= y <len(grid[0]):17 newGrid[x][y]="."1819 xi =[0,0,0,1,-1]20 yi =[0,-1,1,0,0]2122for x inrange(len(grid)):23for y inrange(len(grid[0])):24# check for bomb25if grid[x][y]=="O":26# detonate the cell by calling the function27for i, j inzip(xi, yi):28 detonate(x + i, y + j)2930 grid = newGrid
3132return["".join(x)for x in grid]