you are given a m x n 2d grid initialized with these three possible values 1 a wall or an obstacle 0 a gate inf infinity means an empty room we use the value 231 1 2147483647 to represent inf as you may assume that the distance to a gate is less than 2147483647 fill the empty room with distance to its nearest gate if it is impossible to reach a gate it should be filled with inf for example given the 2d grid inf 1 0 inf inf inf inf 1 inf 1 inf 1 0 1 inf inf after running your function the 2d grid should be 3 1 0 1 2 2 1 1 1 1 2 1 0 1 3 4 out of bounds crossed
def walls_and_gates(rooms):
    for i in range(len(rooms)):
        for j in range(len(rooms[0])):
            if rooms[i][j] == 0:
                dfs(rooms, i, j, 0)


def dfs(rooms, i, j, depth):
    if (i < 0 or i >= len(rooms)) or (j < 0 or j >= len(rooms[0])):
        return  
    if rooms[i][j] < depth:
        return  
    rooms[i][j] = depth
    dfs(rooms, i+1, j, depth+1)
    dfs(rooms, i-1, j, depth+1)
    dfs(rooms, i, j+1, depth+1)
    dfs(rooms, i, j-1, depth+1)