input: 
  def solve_maze(n: int, m: int, grid: List[List[int]]) -> List[int]:
      """
      Solve a given maze represented by a 2D grid of size n x m. The maze is
      represented by a binary matrix where 1 represents a path and 0 represents a
      wall. The goal is to find a path from the top-left corner (0, 0) to the
      bottom-right corner (n-1, m-1). The function returns a list of integers
      representing the path taken to reach the end. If no path is found, the
      function returns an empty list.
      
      >>> solve_maze(3, 3, [[1, 0, 0], [1, 1, 0], [0, 1, 1]])
      [5, 4, 3, 2, 1, 2]
      >>> solve_maze(3, 3, [[1, 0, 0], [1, 0, 0], [1, 1, 1]])
      [5, 4, 3, 2, 1, 2]
      """
  
output:     
      if grid[0][0] == 0 or grid[n-1][m-1] == 0:
          return []
  
      directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
      queue = [(0, 0, [])]
      visited = set()
  
      while queue:
          x, y, path = queue.pop(0)
          if (x, y) == (n-1, m-1):
              return path + [len(path) + 1]
          visited.add((x, y))
  
          for dx, dy in directions:
              nx, ny = x + dx, y + dy
              if 0 <= nx < n and 0 <= ny < m and grid[nx][ny] == 1 and (nx, ny) not in visited:
                  queue.append((nx, ny, path + [len(path) + 1]))
  
      return []
  