input: 
  def solve_maze(n: int, m: int, grid: List[List[int]]) -> List[int]:
      """Given dimensions n and m of a maze (grid of 1s and 0s) find the shortest path 
      from the top-left corner (0, 0) to the bottom-right corner (n-1, m-1). 
      Return the path lengths which consist of cells only containing 1s. If more than one
      path has the same minimum length, any path is acceptable. 
      >>> solve_maze(3, 3, [[1, 0, 1], [1, 1, 1], [0, 1, 1]])
      [1, 2, 1, 1, 1]
      >>> solve_maze(3, 3, [[1, 1, 1], [1, 0, 0], [1, 1, 1]])
      [1, 1, 1, 2, 1, 1]
      """
  
output:     from collections import deque
  
      def is_valid(x, y):
          return 0 <= x < n and 0 <= y < m and grid[x][y] == 1
  
      directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
      queue = deque([(0, 0, [1])])
      visited = set((0, 0))
  
      while queue:
          x, y, path = queue.popleft()
  
          if (x, y) == (n - 1, m - 1):
              return path
  
          for dx, dy in directions:
              nx, ny = x + dx, y + dy
              if is_valid(nx, ny) and (nx, ny) not in visited:
                  visited.add((nx, ny))
                  queue.append((nx, ny, path + [grid[nx][ny]]))