input: 
  def solve_maze(n: int, m: int, grid: List[List[int]]) -> List[int]:
      """ Solves a maze represented by a 2D grid of n rows and m columns, where the start is at the top-left corner and the end is at the bottom-right corner.
      The grid is filled with 1s representing open paths and 0s representing walls. The function returns a list of distances at each step from the start to the end if a path exists.
      If no path exists, the function should return an empty list.
  
      >>> solve_maze(3, 3, [[1, 0, 1], [1, 1, 0], [0, 1, 1]])
      [1, 1, 2, 3, 4]
      >>> solve_maze(3, 3, [[1, 0, 0], [0, 1, 0], [0, 0, 1]])
      []
      """
  
output:     from collections import deque
  
      if grid[0][0] == 0 or grid[n-1][m-1] == 0:
          return []
  
      directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
      queue = deque([(0, 0, 1)])
      visited = [[False] * m for _ in range(n)]
      visited[0][0] = True
      result = []
  
      while queue:
          x, y, dist = queue.popleft()
          result.append(dist)
  
          if x == n - 1 and y == m - 1:
              return result
  
          for dx, dy in directions:
              nx, ny = x + dx, y + dy
              if 0 <= nx < n and 0 <= ny < m and not visited[nx][ny] and grid[nx][ny] == 1:
                  visited[nx][ny] = True
                  queue.append((nx, ny, dist + 1))
  
      return []