fio = open("maze.txt", "r")

maze_raw = fio.readlines()

fio.close()

xmax = len(maze_raw) - 1
ymax = len(maze_raw[0].strip()) - 1

maze = {}
sol = {}
for x in range(len(maze_raw)):
    for y in range(len(maze_raw[x].strip())):
        paths = set()

        if x > 0:
            if maze_raw[x - 1][y] == " ":
                paths.add((x - 1, y))
        
        if y > 0:
            if maze_raw[x][y - 1] == " ":
                paths.add((x, y - 1))
        
        if maze_raw[x][y] == " ":
            paths.add((x, y))
        
        if y < ymax:
            if maze_raw[x][y + 1] == " ":
                paths.add((x, y + 1))
        
        if x < xmax:        
            if maze_raw[x + 1][y] == " ":
                paths.add((x + 1, y))

        if len(paths) != 0:
            maze[(x,y)] = paths
        
        if maze_raw[x][y] == "*":
            sol[(x,y)] = "*"
        else:
            sol[(x,y)] = "?"

for key in maze:
    if key[0] == 0:
        if maze_raw[key[0]][key[1]] == " ":
            sol[key] = "N"
    
    if key[0] == xmax:
        if maze_raw[key[0]][key[1]] == " ":
            sol[key] = "S"
    
    if key[1] == 0:
        if maze_raw[key[0]][key[1]] == " ":
            sol[key] = "W"
    
    if key[1] == ymax:
        if maze_raw[key[0]][key[1]] == " ":
            sol[key] = "E"

cont = True

def check(xi, yi):
    global sol
    return (xi, yi) in sol and sol[xi, yi] != "?" and sol[xi, yi] != "*"

while cont:
    cont = False
    for key in maze:
        if sol[key] == "?":
            if check(key[0] - 1, key[1]):
                sol[key] = "N"
                cont = True
                continue
                
            if check(key[0] + 1, key[1]):
                sol[key] = "S"
                cont = True
                continue
            
            if check(key[0], key[1] - 1):
                sol[key] = "W"
                cont = True
                continue

            if check(key[0], key[1] + 1):
                sol[key] = "E"
                cont = True
                continue


for x in range(xmax+1):
    for y in range(ymax+1):
        print(sol[(x,y)],end="")
    
    print()