input = open("QuadrilleVille.txt").read()
depl = 1000

# input = """ESESS
# NOEON
# NOEEO
# NONEN
# EONON"""
# depl = 7

input = input.replace('N', '0').replace('E', '1').replace('S', '2').replace('O', '3').splitlines()
for i in range(len(input)):
    input[i] = [int(x) for x in input[i]]


#position de départ
x = len(input)//2
y = x

min_x = x
min_y = y
max_x = x
max_y = y

histo = [(x,y), ]

def print_table(table):
    for i in table:
        print("".join([str(x) for x in i]))
    print()

def update_trace(x, y):
    global trace
    global trace_id
    trace[y][x] = trace_id
    trace_id = (trace_id+1)%10

def change_dir(c,x,y,dim):
    c = (c + 1)%4
    for i in range(2):
        if x==0 and c==3:
            c = 0
        if x==dim-1 and c==1:
            c = 2
        if y==0 and c==0:
            c = 1
        if y==dim-1 and c==2:
            c = 3
    return c

trace = list()
for i in range(len(input)):
    trace.append([".", ] * len(input))
trace_id = 0

for i in range(depl):
    update_trace(x,y)
    # print_table(input)
    d = input[y][x]
    input[y][x] = change_dir(d, x, y, len(input))
    if d == 1:
        x+=1
    elif d == 2:
        y+=1
    elif d == 3:
        x-=1
    elif d == 0:
        y-=1
    histo.append((x,y))
    min_x = min(min_x, x)
    min_y = min(min_y, y)
    max_x = max(max_x, x)
    max_y = max(max_y, y)
update_trace(x,y)

print_table(trace)

print("{}, {}, {}, {}, {}".format(len(set(histo)), min_x+1, max_x+1, min_y+1, max_y+1))
