from typing import List, Optional


class Cell:
    def __init__(self, x: int, y: int):
        self.x = x
        self.y = y
        self.is_wall = False
        self.is_start = False
        self.is_exit = False
    
    def is_passable(self) -> bool:
        return not self.is_wall
    
    def __repr__(self) -> str:
        if self.is_start:
            return "S"
        elif self.is_exit:
            return "E"
        elif self.is_wall:
            return "#"
        else:
            return "."
    
    def __eq__(self, other) -> bool:
        if not isinstance(other, Cell):
            return False
        return self.x == other.x and self.y == other.y
    
    def __hash__(self) -> int:
        return hash((self.x, self.y))
    
    def __lt__(self, other):
        return (self.x, self.y) < (other.x, other.y)


class Maze:
    def __init__(self, width: int, height: int):
        self.width = width
        self.height = height
        self._cells: List[List[Cell]] = []
        self.start_cell: Optional[Cell] = None
        self.exit_cell: Optional[Cell] = None
        
        for y in range(height):
            row = []
            for x in range(width):
                row.append(Cell(x, y))
            self._cells.append(row)
    
    def get_cell(self, x: int, y: int) -> Optional[Cell]:
        if 0 <= x < self.width and 0 <= y < self.height:
            return self._cells[y][x]
        return None
    
    def get_neighbors(self, cell: Cell) -> List[Cell]:
        neighbors = []
        directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]
        for dx, dy in directions:
            nx, ny = cell.x + dx, cell.y + dy
            neighbor = self.get_cell(nx, ny)
            if neighbor and neighbor.is_passable():
                neighbors.append(neighbor)
        return neighbors
    
    def set_wall(self, x: int, y: int, is_wall: bool = True) -> None:
        cell = self.get_cell(x, y)
        if cell:
            cell.is_wall = is_wall
    
    def set_start(self, x: int, y: int) -> None:
        cell = self.get_cell(x, y)
        if cell:
            if self.start_cell:
                self.start_cell.is_start = False
            cell.is_start = True
            self.start_cell = cell
    
    def set_exit(self, x: int, y: int) -> None:
        cell = self.get_cell(x, y)
        if cell:
            if self.exit_cell:
                self.exit_cell.is_exit = False
            cell.is_exit = True
            self.exit_cell = cell
    
    def display(self) -> None:
        print("+" + "-" * self.width + "+")
        for y in range(self.height):
            row_str = "|"
            for x in range(self.width):
                cell = self._cells[y][x]
                if cell.is_start:
                    row_str += "S"
                elif cell.is_exit:
                    row_str += "E"
                elif cell.is_wall:
                    row_str += "#"
                else:
                    row_str += " "
            row_str += "|"
            print(row_str)
        print("+" + "-" * self.width + "+")