Algorithm

Rotating the Box

Solution
# Apply gravity column by column
        for j in range(m):
            for i in range(n - 1, -1, -1):
                if result[i][j] == ".":
                    next_row_with_stone = -1
                    
                    # Look for a stone above
                    for k in range(i - 1, -1, -1):
                        if result[k][j] == "*": break
                        if result[k][j] == "#":
                            next_row_with_stone = k
                            break

                    if next_row_with_stone != -1:
                        result[i][j] = "#"
                        result[next_row_with_stone][j] = "."
        return result