input: def solve_maze(n: int, m: int, grid: List[List[int]]) -> List[int]:
      """Given a grid that represents a maze, with '1' indicating a path and '0' 
      indicating a wall, this function finds a path from the top-left corner (0,0) 
      to the bottom-right corner (n-1, m-1) and returns the lengths of the segments
      of the path.
      
      >>> solve_maze(3, 3, [[1,0,1], [1,1,1], [0,1,1]])
      [5]
      >>> solve_maze(2, 2, [[1,1], [1,1]])
      [3]
      """
      maze = Maze(n, m)
      maze._grid = grid
      return maze.solve_maze()
output:     class Maze:
          def __init__(self, n, m):
              self.n = n
              self.m = m
              self.grid = [[0] * m for _ in range(n)]
              self.visited = [[False] * m for _ in range(n)]
              self.path_lengths = []
              self.current_length = 0
          
          def solve_maze(self):
              if self._dfs(0, 0):
                  return self.path_lengths
              return []
          
          def _dfs(self, x, y):
              if x == self.n - 1 and y == self.m - 1:
                  self.path_lengths.append(self.current_length + 1)
                  return True
              
              if x < 0 or x >= self.n or y < 0 or y >= self.m or self.grid[x][y] == 0 or self.visited[x][y]:
                  return False
              
              self.visited[x][y] = True
              self.current_length += 1
              
              if self._dfs(x + 1, y) or self._dfs(x - 1, y) or self._dfs(x, y + 1) or self._dfs(x, y - 1):
                  self.path_lengths.append(self.current_length)
                  return True
              
              self.current_length -= 1
              self.visited[x][y] = False
              return False