Buggy python submission (verdict: Runtime Error):
import copy
def hinan():
    ans = []
    while True:
        n = raw_input().split(" ")
        if n == ["0" ,"0"]:
            for an in ans:
                print an
            break
        elif len(n) == 2:
            xs = []
            k = int(n[1])
            for m in range(k):
                n = map(str,raw_input())
                xs.append(n)
            for time in range(181):
                check = []
                for i in range(len(xs)):
                    if not any(x in xs[i] for x in ["W","E","N","S"]):
                        check.append(True)
                        continue
                    else:
                        check.append(False)
                    for j in range(len(xs[i])):
                        if xs[i][j] == "S":
                            if xs[i][j - 1] in ["." ,"X"]:
                                xs[i][j] = "W"
                            elif xs[i + 1][j] in ["." ,"X"]:
                                xs[i][j] = "S"
                            elif xs[i][j + 1] in ["." ,"X"]:
                                xs[i][j] = "E"
                            elif xs[i - 1][j] in ["." ,"X"]:
                                xs[i][j] = "N"
                        elif xs[i][j] == "W":
                            if xs[i - 1][j] in ["." ,"X"]:
                                xs[i][j] = "N"
                            elif xs[i][j - 1] in ["." ,"X"]:
                                xs[i][j] = "W"
                            elif xs[i + 1][j] in ["." ,"X"]:
                                xs[i][j] = "S"
                            elif xs[i][j + 1] in ["." ,"X"]:
                                xs[i][j] = "E"
                        elif xs[i][j] == "E":
                            if xs[i + 1][j] in ["." ,"X"]:
                                xs[i][j] = "S"
                            elif xs[i][j + 1] in ["." ,"X"]:
                                xs[i][j] = "E"
                            elif xs[i - 1][j] in ["." ,"X"]:
                                xs[i][j] = "N"
                            elif xs[i][j - 1] in ["." ,"X"]:
                                xs[i][j] = "W"
                        elif xs[i][j] == "N":
                            if xs[i][j + 1] in ["." ,"X"]:
                                xs[i][j] = "E"
                            elif xs[i - 1][j] in ["." ,"X"]:
                                xs[i][j] = "N"
                            elif xs[i][j - 1] in ["." ,"X"]:
                                xs[i][j] = "W"
                            elif xs[i + 1][j] in ["." ,"X"]:
                                xs[i][j] = "S"
                ys = copy.deepcopy(xs)
                if all(check):
                    ans.append(time)
                    break
                for i in range(len(xs)):
                    for j in range(len(xs[i])):
                        if xs[i][j] == "S":
                            if xs[i + 1][j + 1] != "W":
                                if xs[i + 1][j] == "X":
                                    ys[i][j] = "."
                                    continue
                                else:
                                    ys[i + 1][j] ,ys[i][j] = "S" ,"."
                                    continue
                        elif xs[i][j] == "E":
                            if j < len(xs[i]) - 2:
                                if xs[i][j + 2] != "W" and xs[i - 1][j + 1] != "S":
                                    if xs[i][j + 1] == "X":
                                        ys[i][j] = "."
                                        continue
                                    else:
                                        ys[i][j + 1] ,ys[i][j] = "E" ,"."
                                        continue
                            else:
                                if xs[i][j + 1] == "X":
                                    ys[i][j] = "."
                                    continue
                                else:
                                    ys[i][j + 1] ,ys[i][j] = "E" ,"."
                                    continue
                        elif xs[i][j] == "W":
                            if xs[i][j - 1] == "X":
                                ys[i][j] = "."
                                continue
                            else:
                                ys[i][j - 1] ,ys[i][j] = "W" ,"."
                                continue
                        elif xs[i][j] == "N":
                            if xs[i - 1][j + 1] != "W" and xs[i - 2][j] != "S" and xs[i - 1][j - 1] != "E":
                                if xs[i - 1][j] == "X":
                                    ys[i][j] = "."
                                    continue
                                elif xs[i - 1][j] == ".":
                                    ys[i - 1][j] ,ys[i][j] = "N" ,"."
                                    continue
                xs = ys
            else:
                ans.append("NA")
hinan()

Fixed submission:
def hinan():
    ans ,houi ,vector = [] ,[[0 ,1] ,[-1 ,0] ,[0 ,-1] ,[1 ,0] ,[0 ,1] ,[-1 , 0] ,[1 ,0]] ,["E" ,"N" ,"W" ,"S"]
    while True:
        n = raw_input().split(" ")
        if n == ["0" ,"0"]:
            for an in ans:
                print an
            break
        else:
            xs =[[] ,[] ,[]]
            for i in range(int(n[1])):
                n = raw_input()
                xs = [xs[l] + [[[i ,j] for j in range(len(n)) if n[j] != "#"] ,[[[i ,j] ,houi[vector.index(n[j])]] for j in range(len(n)) if n[j] in vector] ,[[i ,j] for j in range(len(n)) if n[j] == "X"]][l] for l in range(3)]
            for time in range(181):
                if len(xs[1]) == 0:
                    ans.append(time)
                    break
                else:
                    for i in xs[1]:
                        where = [l[0] for l in xs[1]]
                        if len([k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where]) > 0:
                            i[1] = [[k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where][0][s] - i[0][s] for s in range(2)]
                    go_point = [[i[0][l] + i[1][l] for l in range(2)] for i in xs[1]]
                    for i in range(len(go_point)):
                        if go_point[i] in where or not go_point[i] in xs[0]:
                            go_point[i] = xs[1][i][0]
                        else:
                            if go_point.count(go_point[i]) > 1:
                                for j in range(4):
                                    if xs[1][i] == [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]]:
                                        break
                                    elif [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]] in xs[1]:
                                        go_point[i] = xs[1][i][0]
                                        break
                    else:
                        xs[1] = [k for k in [[go_point[i] ,xs[1][i][1]] for i in range(len(xs[1]))] if not k[0] in xs[2]]
            else:
                ans.append("NA")
hinan()

Buggy python submission (verdict: Runtime Error):
import copy
def hinan():
    ans = []
    while True:
        n = raw_input().split(" ")
        if n == ["0" ,"0"]:
            for an in ans:
                print an
            break
        elif len(n) == 2:
            xs = []
            k = int(n[1])
            for m in range(k):
                n = map(str,raw_input())
                xs.append(n)
            for time in range(181):
                check = []
                for i in range(len(xs)):
                    if not any(x in xs[i] for x in ["W","E","N","S"]):
                        check.append(True)
                        continue
                    else:
                        check.append(False)
                    for j in range(len(xs[i])):
                        if xs[i][j] == "S":
                            if xs[i][j - 1] in ["." ,"X"]:
                                xs[i][j] = "W"
                            elif xs[i + 1][j] in ["." ,"X"]:
                                xs[i][j] = "S"
                            elif xs[i][j + 1] in ["." ,"X"]:
                                xs[i][j] = "E"
                            elif xs[i - 1][j] in ["." ,"X"]:
                                xs[i][j] = "N"
                        elif xs[i][j] == "W":
                            if xs[i - 1][j] in ["." ,"X"]:
                                xs[i][j] = "N"
                            elif xs[i][j - 1] in ["." ,"X"]:
                                xs[i][j] = "W"
                            elif xs[i + 1][j] in ["." ,"X"]:
                                xs[i][j] = "S"
                            elif xs[i][j + 1] in ["." ,"X"]:
                                xs[i][j] = "E"
                        elif xs[i][j] == "E":
                            if xs[i + 1][j] in ["." ,"X"]:
                                xs[i][j] = "S"
                            elif xs[i][j + 1] in ["." ,"X"]:
                                xs[i][j] = "E"
                            elif xs[i - 1][j] in ["." ,"X"]:
                                xs[i][j] = "N"
                            elif xs[i][j - 1] in ["." ,"X"]:
                                xs[i][j] = "W"
                        elif xs[i][j] == "N":
                            if xs[i][j + 1] in ["." ,"X"]:
                                xs[i][j] = "E"
                            elif xs[i - 1][j] in ["." ,"X"]:
                                xs[i][j] = "N"
                            elif xs[i][j - 1] in ["." ,"X"]:
                                xs[i][j] = "W"
                            elif xs[i + 1][j] in ["." ,"X"]:
                                xs[i][j] = "S"
                ys = copy.deepcopy(xs)
                if all(check):
                    ans.append(time)
                    break
                for i in range(len(xs)):
                    for j in range(len(xs[i])):
                        if xs[i][j] == "S":
                            if xs[i + 1][j + 1] != "W":
                                if xs[i + 1][j] == "X":
                                    ys[i][j] = "."
                                    continue
                                else:
                                    ys[i + 1][j] ,ys[i][j] = "S" ,"."
                                    continue
                        elif xs[i][j] == "E":
                            if j < len(xs[i]) - 2:
                                if xs[i][j + 2] != "W" and xs[i - 1][j + 1] != "S":
                                    if xs[i][j + 1] == "X":
                                        ys[i][j] = "."
                                        continue
                                    else:
                                        ys[i][j + 1] ,ys[i][j] = "E" ,"."
                                        continue
                            else:
                                if xs[i][j + 1] == "X":
                                    ys[i][j] = "."
                                    continue
                                else:
                                    ys[i][j + 1] ,ys[i][j] = "E" ,"."
                                    continue
                        elif xs[i][j] == "W":
                            if xs[i][j - 1] == "X":
                                ys[i][j] = "."
                                continue
                            else:
                                ys[i][j - 1] ,ys[i][j] = "W" ,"."
                                continue
                        elif xs[i][j] == "N":
                            if xs[i - 1][j + 1] != "W" and xs[i - 2][j] != "S" and xs[i - 1][j - 1] != "E":
                                if xs[i - 1][j] == "X":
                                    ys[i][j] = "."
                                    continue
                                elif xs[i - 1][j] == ".":
                                    ys[i - 1][j] ,ys[i][j] = "N" ,"."
                                    continue
                xs = ys
            else:
                ans.append("NA")
hinan()

Fixed submission:
def hinan():
    ans ,houi ,vector = [] ,[[0 ,1] ,[-1 ,0] ,[0 ,-1] ,[1 ,0] ,[0 ,1] ,[-1 , 0] ,[1 ,0]] ,["E" ,"N" ,"W" ,"S"]
    while True:
        n = raw_input().split(" ")
        if n == ["0" ,"0"]:
            for an in ans:
                print an
            break
        else:
            xs =[[] ,[] ,[]]
            for i in range(int(n[1])):
                n = raw_input()
                xs = [xs[l] + [[[i ,j] for j in range(len(n)) if n[j] != "#"] ,[[[i ,j] ,houi[vector.index(n[j])]] for j in range(len(n)) if n[j] in vector] ,[[i ,j] for j in range(len(n)) if n[j] == "X"]][l] for l in range(3)]
            for time in range(181):
                if len(xs[1]) == 0:
                    ans.append(time)
                    break
                else:
                    for i in xs[1]:
                        where = [l[0] for l in xs[1]]
                        if len([k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where]) > 0:
                            i[1] = [[k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where][0][s] - i[0][s] for s in range(2)]
                    go_point = [[i[0][l] + i[1][l] for l in range(2)] for i in xs[1]]
                    for i in range(len(go_point)):
                        if go_point[i] in where or not go_point[i] in xs[0]:
                            go_point[i] = xs[1][i][0]
                        else:
                            if go_point.count(go_point[i]) > 1:
                                for j in range(4):
                                    if xs[1][i] == [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]]:
                                        break
                                    elif [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]] in xs[1]:
                                        go_point[i] = xs[1][i][0]
                                        break
                    else:
                        xs[1] = [k for k in [[go_point[i] ,xs[1][i][1]] for i in range(len(xs[1]))] if not k[0] in xs[2]]
            else:
                ans.append("NA")
hinan()

Buggy python submission (verdict: Runtime Error):
import copy
def hinan():
    ans = []
    while True:
        n = raw_input().split(" ")
        if n == ["0" ,"0"]:
            for an in ans:
                print an
            break
        elif len(n) == 2:
            xs = []
            k = int(n[1])
            for m in range(k):
                n = map(str,raw_input())
                xs.append(n)
            for time in range(181):
                check = []
                for i in range(len(xs)):
                    if not any(x in xs[i] for x in ["W","E","N","S"]):
                        check.append(True)
                        continue
                    else:
                        check.append(False)
                    for j in range(len(xs[i])):
                        if xs[i][j] == "S":
                            if xs[i][j - 1] in ["." ,"X"]:
                                xs[i][j] = "W"
                            elif xs[i + 1][j] in ["." ,"X"]:
                                xs[i][j] = "S"
                            elif xs[i][j + 1] in ["." ,"X"]:
                                xs[i][j] = "E"
                            elif xs[i - 1][j] in ["." ,"X"]:
                                xs[i][j] = "N"
                        elif xs[i][j] == "W":
                            if xs[i - 1][j] in ["." ,"X"]:
                                xs[i][j] = "N"
                            elif xs[i][j - 1] in ["." ,"X"]:
                                xs[i][j] = "W"
                            elif xs[i + 1][j] in ["." ,"X"]:
                                xs[i][j] = "S"
                            elif xs[i][j + 1] in ["." ,"X"]:
                                xs[i][j] = "E"
                        elif xs[i][j] == "E":
                            if xs[i + 1][j] in ["." ,"X"]:
                                xs[i][j] = "S"
                            elif xs[i][j + 1] in ["." ,"X"]:
                                xs[i][j] = "E"
                            elif xs[i - 1][j] in ["." ,"X"]:
                                xs[i][j] = "N"
                            elif xs[i][j - 1] in ["." ,"X"]:
                                xs[i][j] = "W"
                        elif xs[i][j] == "N":
                            if xs[i][j + 1] in ["." ,"X"]:
                                xs[i][j] = "E"
                            elif xs[i - 1][j] in ["." ,"X"]:
                                xs[i][j] = "N"
                            elif xs[i][j - 1] in ["." ,"X"]:
                                xs[i][j] = "W"
                            elif xs[i + 1][j] in ["." ,"X"]:
                                xs[i][j] = "S"
                ys = copy.deepcopy(xs)
                if all(check):
                    ans.append(time)
                    break
                for i in range(len(xs)):
                    for j in range(len(xs[i])):
                        if xs[i][j] == "S":
                            if xs[i + 1][j + 1] != "W":
                                if xs[i + 1][j] == "X":
                                    ys[i][j] = "."
                                    continue
                                else:
                                    ys[i + 1][j] ,ys[i][j] = "S" ,"."
                                    continue
                        elif xs[i][j] == "E":
                            if j < len(xs[i]) - 2:
                                if xs[i][j + 2] != "W" and xs[i - 1][j + 1] != "S":
                                    if xs[i][j + 1] == "X":
                                        ys[i][j] = "."
                                        continue
                                    else:
                                        ys[i][j + 1] ,ys[i][j] = "E" ,"."
                                        continue
                            else:
                                if xs[i][j + 1] == "X":
                                    ys[i][j] = "."
                                    continue
                                else:
                                    ys[i][j + 1] ,ys[i][j] = "E" ,"."
                                    continue
                        elif xs[i][j] == "W":
                            if xs[i][j - 1] == "X":
                                ys[i][j] = "."
                                continue
                            else:
                                ys[i][j - 1] ,ys[i][j] = "W" ,"."
                                continue
                        elif xs[i][j] == "N":
                            if xs[i - 1][j + 1] != "W" and xs[i - 2][j] != "S" and xs[i - 1][j - 1] != "E":
                                if xs[i - 1][j] == "X":
                                    ys[i][j] = "."
                                    continue
                                elif xs[i - 1][j] == ".":
                                    ys[i - 1][j] ,ys[i][j] = "N" ,"."
                                    continue
                xs = ys
            else:
                ans.append("NA")
hinan()

Fixed submission:
def hinan():
    ans ,houi ,vector = [] ,[[0 ,1] ,[-1 ,0] ,[0 ,-1] ,[1 ,0] ,[0 ,1] ,[-1 , 0] ,[1 ,0]] ,["E" ,"N" ,"W" ,"S"]
    while True:
        n = raw_input().split(" ")
        if n == ["0" ,"0"]:
            for an in ans:
                print an
            break
        else:
            xs =[[] ,[] ,[]]
            for i in range(int(n[1])):
                n = raw_input()
                xs = [xs[l] + [[[i ,j] for j in range(len(n)) if n[j] != "#"] ,[[[i ,j] ,houi[vector.index(n[j])]] for j in range(len(n)) if n[j] in vector] ,[[i ,j] for j in range(len(n)) if n[j] == "X"]][l] for l in range(3)]
            for time in range(181):
                if len(xs[1]) == 0:
                    ans.append(time)
                    break
                else:
                    for i in xs[1]:
                        where = [l[0] for l in xs[1]]
                        if len([k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where]) > 0:
                            i[1] = [[k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where][0][s] - i[0][s] for s in range(2)]
                    go_point = [[i[0][l] + i[1][l] for l in range(2)] for i in xs[1]]
                    for i in range(len(go_point)):
                        if go_point[i] in where or not go_point[i] in xs[0]:
                            go_point[i] = xs[1][i][0]
                        else:
                            if go_point.count(go_point[i]) > 1:
                                for j in range(4):
                                    if xs[1][i] == [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]]:
                                        break
                                    elif [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]] in xs[1]:
                                        go_point[i] = xs[1][i][0]
                                        break
                    else:
                        xs[1] = [k for k in [[go_point[i] ,xs[1][i][1]] for i in range(len(xs[1]))] if not k[0] in xs[2]]
            else:
                ans.append("NA")
hinan()

Buggy python submission (verdict: Runtime Error):
import copy
def hinan():
    ans = []
    while True:
        n = raw_input().split(" ")
        if n == ["0" ,"0"]:
            for an in ans:
                print an
            break
        elif len(n) == 2:
            xs = []
            k = int(n[1])
            for m in range(k):
                n = map(str,raw_input())
                xs.append(n)
            for time in range(181):
                check = []
                for i in range(len(xs)):
                    if not any(x in xs[i] for x in ["W","E","N","S"]):
                        check.append(True)
                        continue
                    else:
                        check.append(False)
                    for j in range(len(xs[i])):
                        if xs[i][j] == "S":
                            if xs[i][j - 1] in ["." ,"X"]:
                                xs[i][j] = "W"
                            elif xs[i + 1][j] in ["." ,"X"]:
                                xs[i][j] = "S"
                            elif xs[i][j + 1] in ["." ,"X"]:
                                xs[i][j] = "E"
                            elif xs[i - 1][j] in ["." ,"X"]:
                                xs[i][j] = "N"
                        elif xs[i][j] == "W":
                            if xs[i - 1][j] in ["." ,"X"]:
                                xs[i][j] = "N"
                            elif xs[i][j - 1] in ["." ,"X"]:
                                xs[i][j] = "W"
                            elif xs[i + 1][j] in ["." ,"X"]:
                                xs[i][j] = "S"
                            elif xs[i][j + 1] in ["." ,"X"]:
                                xs[i][j] = "E"
                        elif xs[i][j] == "E":
                            if xs[i + 1][j] in ["." ,"X"]:
                                xs[i][j] = "S"
                            elif xs[i][j + 1] in ["." ,"X"]:
                                xs[i][j] = "E"
                            elif xs[i - 1][j] in ["." ,"X"]:
                                xs[i][j] = "N"
                            elif xs[i][j - 1] in ["." ,"X"]:
                                xs[i][j] = "W"
                        elif xs[i][j] == "N":
                            if xs[i][j + 1] in ["." ,"X"]:
                                xs[i][j] = "E"
                            elif xs[i - 1][j] in ["." ,"X"]:
                                xs[i][j] = "N"
                            elif xs[i][j - 1] in ["." ,"X"]:
                                xs[i][j] = "W"
                            elif xs[i + 1][j] in ["." ,"X"]:
                                xs[i][j] = "S"
                ys = copy.deepcopy(xs)
                if all(check):
                    ans.append(time)
                    break
                for i in range(len(xs)):
                    for j in range(len(xs[i])):
                        if xs[i][j] == "S":
                            if xs[i + 1][j + 1] != "W":
                                if xs[i + 1][j] == "X":
                                    ys[i][j] = "."
                                    continue
                                else:
                                    ys[i + 1][j] ,ys[i][j] = "S" ,"."
                                    continue
                        elif xs[i][j] == "E":
                            if j < len(xs[i]) - 2:
                                if xs[i][j + 2] != "W" and xs[i - 1][j + 1] != "S":
                                    if xs[i][j + 1] == "X":
                                        ys[i][j] = "."
                                        continue
                                    else:
                                        ys[i][j + 1] ,ys[i][j] = "E" ,"."
                                        continue
                            else:
                                if xs[i][j + 1] == "X":
                                    ys[i][j] = "."
                                    continue
                                else:
                                    ys[i][j + 1] ,ys[i][j] = "E" ,"."
                                    continue
                        elif xs[i][j] == "W":
                            if xs[i][j - 1] == "X":
                                ys[i][j] = "."
                                continue
                            else:
                                ys[i][j - 1] ,ys[i][j] = "W" ,"."
                                continue
                        elif xs[i][j] == "N":
                            if i > 1:
                                if xs[i - 1][j + 1] != "W" and xs[i - 2][j] != "S" and xs[i - 1][j - 1] != "E":
                                    if xs[i - 1][j] == "X":
                                        ys[i][j] = "."
                                        continue
                                    elif xs[i - 1][j] == ".":
                                        ys[i - 1][j] ,ys[i][j] = "N" ,"."
                                        continue
                            else:
                                if xs[i - 1][j + 1] != "W" and xs[i - 1][j - 1] != "E":
                                    if xs[i - 1][j] == "X":
                                        ys[i][j] = "."
                                        continue
                                    elif xs[i - 1][j] == ".":
                                        ys[i - 1][j] ,ys[i][j] = "N" ,"."
                                        continue
                xs = ys
            else:
                ans.append("NA")
hinan()

Fixed submission:
def hinan():
    ans ,houi ,vector = [] ,[[0 ,1] ,[-1 ,0] ,[0 ,-1] ,[1 ,0] ,[0 ,1] ,[-1 , 0] ,[1 ,0]] ,["E" ,"N" ,"W" ,"S"]
    while True:
        n = raw_input().split(" ")
        if n == ["0" ,"0"]:
            for an in ans:
                print an
            break
        else:
            xs =[[] ,[] ,[]]
            for i in range(int(n[1])):
                n = raw_input()
                xs = [xs[l] + [[[i ,j] for j in range(len(n)) if n[j] != "#"] ,[[[i ,j] ,houi[vector.index(n[j])]] for j in range(len(n)) if n[j] in vector] ,[[i ,j] for j in range(len(n)) if n[j] == "X"]][l] for l in range(3)]
            for time in range(181):
                if len(xs[1]) == 0:
                    ans.append(time)
                    break
                else:
                    for i in xs[1]:
                        where = [l[0] for l in xs[1]]
                        if len([k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where]) > 0:
                            i[1] = [[k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where][0][s] - i[0][s] for s in range(2)]
                    go_point = [[i[0][l] + i[1][l] for l in range(2)] for i in xs[1]]
                    for i in range(len(go_point)):
                        if go_point[i] in where or not go_point[i] in xs[0]:
                            go_point[i] = xs[1][i][0]
                        else:
                            if go_point.count(go_point[i]) > 1:
                                for j in range(4):
                                    if xs[1][i] == [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]]:
                                        break
                                    elif [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]] in xs[1]:
                                        go_point[i] = xs[1][i][0]
                                        break
                    else:
                        xs[1] = [k for k in [[go_point[i] ,xs[1][i][1]] for i in range(len(xs[1]))] if not k[0] in xs[2]]
            else:
                ans.append("NA")
hinan()

Buggy python submission (verdict: Runtime Error):
def add(x ,y): return x + y
def hinan():
    ans ,houi ,vector = [] ,[[0 ,1] ,[-1 ,0] ,[0 ,-1] ,[1 ,0] ,[0 ,1] ,[-1 , 0] ,[1 ,0]] ,["E" ,"N" ,"W" ,"S"]
    while True:
        n = raw_input().split(" ")
        if n == ["0" ,"0"]:
            for an in ans:
                print an
            break
        else:
            xs =[[] ,[] ,[]]
            for i in range(int(n[1])):
                n = raw_input()
                for k in range(3):
                    xs[k] = xs[k] + [[[i ,j] for j in range(len(n)) if n[j] != "#"] ,[[[i ,j] ,houi[vector.index(n[j])]] for j in range(len(n)) if n[j] in vector] ,[[i ,j] for j in range(len(n)) if n[j] == "X"]][k]
            for time in range(181):
                if len(xs[1]) == 0:
                    ans.append(time)
                    break
                else:
                    for i in xs[1]:
                        for j in range(4):
                            look_point = map(add ,i[0] ,houi[houi.index(i[1]) - 1 + j])
                            if look_point in xs[0]:
                                i[1] = houi[houi.index(i[1]) - 1 + j]
                                break
                    go_point ,where = [] ,[i[0] for i in xs[1]]
                    for i in xs[1]:
                        go_point.append(map(add ,i[0] ,i[1]))
                    for i in range(len(go_point)):
                        if go_point.count(go_point[i]) > 1:
                            if not go_point[i] in where:
                                for j in range(4):
                                    if xs[1][i] == [map(add ,go_point[i] ,houi[j]) ,houi[j + 2]]:
                                        xs[1][i][0] ,where[i] = go_point[i] ,go_point[i]
                                    elif [map(add ,go_point[i] ,houi[j]) ,houi[j + 2]] in xs[1]:
                                        break
                        else:
                            xs[1][i][0] ,where[i] = go_point[i] ,go_point[i]
                        if xs[1][i][0] in xs[2]:
                            del xs[1][i]
                    print xs ,look_point ,go_point ,where
            else:
                ans.append("NA")
hinan()

Fixed submission:
def hinan():
    ans ,houi ,vector = [] ,[[0 ,1] ,[-1 ,0] ,[0 ,-1] ,[1 ,0] ,[0 ,1] ,[-1 , 0] ,[1 ,0]] ,["E" ,"N" ,"W" ,"S"]
    while True:
        n = raw_input().split(" ")
        if n == ["0" ,"0"]:
            for an in ans:
                print an
            break
        else:
            xs =[[] ,[] ,[]]
            for i in range(int(n[1])):
                n = raw_input()
                xs = [xs[l] + [[[i ,j] for j in range(len(n)) if n[j] != "#"] ,[[[i ,j] ,houi[vector.index(n[j])]] for j in range(len(n)) if n[j] in vector] ,[[i ,j] for j in range(len(n)) if n[j] == "X"]][l] for l in range(3)]
            for time in range(181):
                if len(xs[1]) == 0:
                    ans.append(time)
                    break
                else:
                    for i in xs[1]:
                        where = [l[0] for l in xs[1]]
                        if len([k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where]) > 0:
                            i[1] = [[k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where][0][s] - i[0][s] for s in range(2)]
                    go_point = [[i[0][l] + i[1][l] for l in range(2)] for i in xs[1]]
                    for i in range(len(go_point)):
                        if go_point[i] in where or not go_point[i] in xs[0]:
                            go_point[i] = xs[1][i][0]
                        else:
                            if go_point.count(go_point[i]) > 1:
                                for j in range(4):
                                    if xs[1][i] == [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]]:
                                        break
                                    elif [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]] in xs[1]:
                                        go_point[i] = xs[1][i][0]
                                        break
                    else:
                        xs[1] = [k for k in [[go_point[i] ,xs[1][i][1]] for i in range(len(xs[1]))] if not k[0] in xs[2]]
            else:
                ans.append("NA")
hinan()

Buggy python submission (verdict: Runtime Error):
def hinan():
    ans ,houi ,vector = [] ,[[0 ,1] ,[-1 ,0] ,[0 ,-1] ,[1 ,0] ,[0 ,1] ,[-1 , 0] ,[1 ,0]] ,["E" ,"N" ,"W" ,"S"]
    while True:
        n = raw_input().split(" ")
        if n == ["0" ,"0"]:
            for an in ans:
                print an
            break
        else:
            xs =[[] ,[] ,[]]
            for i in range(int(n[1])):
                n = raw_input()
                xs = [xs[l] + [[[i ,j] for j in range(len(n)) if n[j] != "#"] ,[[[i ,j] ,houi[vector.index(n[j])]] for j in range(len(n)) if n[j] in vector] ,[[i ,j] for j in range(len(n)) if n[j] == "X"]][l] for l in range(3)]
            for time in range(181):
                if len(xs[1]) == 0:
                    ans.append(time)
                    break
                else:
                    for i in xs[1]:
                        where ,look_point = [l[0] for l in xs[1]] ,[[k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where]
                        if len(look_point) > 0:
                            i[1] = [look_point[0][s] - i[0][s] for s in range(2)]
                    go_point = [[i[0][l] + i[1][l] for l in range(2)] for i in xs[1]]
                    for i in range(len(go_point)):
                        if go_point.count(go_point[i]) > 1:
                            if not go_point[i] in where:
                                for j in range(4):
                                    if xs[1][i] == [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]]:
                                        xs[1][i][0] ,where[i] = go_point[i] ,go_point[i]
                                    elif [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]] in xs[1]:
                                        break
                        else:
                            xs[1][i][0] ,where[i] = go_point[i] ,go_point[i]
                    xs[1] = [i for i in xs[1] if not i[0] in xs[2]]
            else:
                ans.append("NA")
hinan()

Fixed submission:
def hinan():
    ans ,houi ,vector = [] ,[[0 ,1] ,[-1 ,0] ,[0 ,-1] ,[1 ,0] ,[0 ,1] ,[-1 , 0] ,[1 ,0]] ,["E" ,"N" ,"W" ,"S"]
    while True:
        n = raw_input().split(" ")
        if n == ["0" ,"0"]:
            for an in ans:
                print an
            break
        else:
            xs =[[] ,[] ,[]]
            for i in range(int(n[1])):
                n = raw_input()
                xs = [xs[l] + [[[i ,j] for j in range(len(n)) if n[j] != "#"] ,[[[i ,j] ,houi[vector.index(n[j])]] for j in range(len(n)) if n[j] in vector] ,[[i ,j] for j in range(len(n)) if n[j] == "X"]][l] for l in range(3)]
            for time in range(181):
                if len(xs[1]) == 0:
                    ans.append(time)
                    break
                else:
                    for i in xs[1]:
                        where = [l[0] for l in xs[1]]
                        if len([k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where]) > 0:
                            i[1] = [[k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where][0][s] - i[0][s] for s in range(2)]
                    go_point = [[i[0][l] + i[1][l] for l in range(2)] for i in xs[1]]
                    for i in range(len(go_point)):
                        if go_point[i] in where or not go_point[i] in xs[0]:
                            go_point[i] = xs[1][i][0]
                        else:
                            if go_point.count(go_point[i]) > 1:
                                for j in range(4):
                                    if xs[1][i] == [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]]:
                                        break
                                    elif [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]] in xs[1]:
                                        go_point[i] = xs[1][i][0]
                                        break
                    else:
                        xs[1] = [k for k in [[go_point[i] ,xs[1][i][1]] for i in range(len(xs[1]))] if not k[0] in xs[2]]
            else:
                ans.append("NA")
hinan()

Buggy python submission (verdict: Runtime Error):
def hinan():
    ans ,houi ,vector = [] ,[[0 ,1] ,[-1 ,0] ,[0 ,-1] ,[1 ,0] ,[0 ,1] ,[-1 , 0] ,[1 ,0]] ,["E" ,"N" ,"W" ,"S"]
    while True:
        n = raw_input().split(" ")
        if n == ["0" ,"0"]:
            for an in ans:
                print an
            break
        else:
            xs =[[] ,[] ,[]]
            for i in range(int(n[1])):
                n = raw_input()
                xs = [xs[l] + [[[i ,j] for j in range(len(n)) if n[j] != "#"] ,[[[i ,j] ,houi[vector.index(n[j])]] for j in range(len(n)) if n[j] in vector] ,[[i ,j] for j in range(len(n)) if n[j] == "X"]][l] for l in range(3)]
            for time in range(181):
                print xs
                if len(xs[1]) == 0:
                    ans.append(time)
                    break
                else:
                    for i in xs[1]:
                        where = [l[0] for l in xs[1]]
                        if len([k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where]) > 0:
                            i[1] = [[k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where][0][s] - i[0][s] for s in range(2)]
                    go_point = [[i[0][l] + i[1][l] for l in range(2)] for i in xs[1]]
                    for i in range(len(go_point)):
                        if go_point.count(go_point[i]) > 1:
                            if not go_point[i] in where:
                                for j in range(4):
                                    if xs[1][i] != [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]] and [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]] in xs[1]:
                                        go_point[i] = xs[1][0]
                                        break
                    xs[1] = [[go_point[i] ,xs[1][i][1]] for i in range(len(xs[1])) if not go_point[i] in xs[2]]
            else:
                ans.append("NA")
hinan()

Fixed submission:
def hinan():
    ans ,houi ,vector = [] ,[[0 ,1] ,[-1 ,0] ,[0 ,-1] ,[1 ,0] ,[0 ,1] ,[-1 , 0] ,[1 ,0]] ,["E" ,"N" ,"W" ,"S"]
    while True:
        n = raw_input().split(" ")
        if n == ["0" ,"0"]:
            for an in ans:
                print an
            break
        else:
            xs =[[] ,[] ,[]]
            for i in range(int(n[1])):
                n = raw_input()
                xs = [xs[l] + [[[i ,j] for j in range(len(n)) if n[j] != "#"] ,[[[i ,j] ,houi[vector.index(n[j])]] for j in range(len(n)) if n[j] in vector] ,[[i ,j] for j in range(len(n)) if n[j] == "X"]][l] for l in range(3)]
            for time in range(181):
                if len(xs[1]) == 0:
                    ans.append(time)
                    break
                else:
                    for i in xs[1]:
                        where = [l[0] for l in xs[1]]
                        if len([k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where]) > 0:
                            i[1] = [[k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where][0][s] - i[0][s] for s in range(2)]
                    go_point = [[i[0][l] + i[1][l] for l in range(2)] for i in xs[1]]
                    for i in range(len(go_point)):
                        if go_point[i] in where or not go_point[i] in xs[0]:
                            go_point[i] = xs[1][i][0]
                        else:
                            if go_point.count(go_point[i]) > 1:
                                for j in range(4):
                                    if xs[1][i] == [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]]:
                                        break
                                    elif [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]] in xs[1]:
                                        go_point[i] = xs[1][i][0]
                                        break
                    else:
                        xs[1] = [k for k in [[go_point[i] ,xs[1][i][1]] for i in range(len(xs[1]))] if not k[0] in xs[2]]
            else:
                ans.append("NA")
hinan()

Buggy python submission (verdict: Runtime Error):
def hinan():
    ans ,houi ,vector = [] ,[[0 ,1] ,[-1 ,0] ,[0 ,-1] ,[1 ,0] ,[0 ,1] ,[-1 , 0] ,[1 ,0]] ,["E" ,"N" ,"W" ,"S"]
    while True:
        n = raw_input().split(" ")
        if n == ["0" ,"0"]:
            for an in ans:
                print an
            break
        else:
            xs =[[] ,[] ,[]]
            for i in range(int(n[1])):
                n = raw_input()
                xs = [xs[l] + [[[i ,j] for j in range(len(n)) if n[j] != "#"] ,[[[i ,j] ,houi[vector.index(n[j])]] for j in range(len(n)) if n[j] in vector] ,[[i ,j] for j in range(len(n)) if n[j] == "X"]][l] for l in range(3)]
            for time in range(181):
                if len(xs[1]) == 0:
                    ans.append(time)
                    break
                else:
                    for i in xs[1]:
                        where = [l[0] for l in xs[1]]
                        if len([k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where]) > 0:
                            i[1] = [[k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where][0][s] - i[0][s] for s in range(2)]
                    go_point = [[i[0][l] + i[1][l] for l in range(2)] for i in xs[1]]
                    for i in range(len(go_point)):
                        if go_point.count(go_point[i]) > 1:
                            if not go_point[i] in where:
                                for j in range(4):
                                    if xs[1][i] != [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]] and [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]] in xs[1]:
                                        go_point[i] = xs[1][0]
                                        break
                    xs[1] = [[go_point[i] ,xs[1][i][1]] for i in range(len(xs[1])) if not go_point[i] in xs[2]]
            else:
                ans.append("NA")
hinan()

Fixed submission:
def hinan():
    ans ,houi ,vector = [] ,[[0 ,1] ,[-1 ,0] ,[0 ,-1] ,[1 ,0] ,[0 ,1] ,[-1 , 0] ,[1 ,0]] ,["E" ,"N" ,"W" ,"S"]
    while True:
        n = raw_input().split(" ")
        if n == ["0" ,"0"]:
            for an in ans:
                print an
            break
        else:
            xs =[[] ,[] ,[]]
            for i in range(int(n[1])):
                n = raw_input()
                xs = [xs[l] + [[[i ,j] for j in range(len(n)) if n[j] != "#"] ,[[[i ,j] ,houi[vector.index(n[j])]] for j in range(len(n)) if n[j] in vector] ,[[i ,j] for j in range(len(n)) if n[j] == "X"]][l] for l in range(3)]
            for time in range(181):
                if len(xs[1]) == 0:
                    ans.append(time)
                    break
                else:
                    for i in xs[1]:
                        where = [l[0] for l in xs[1]]
                        if len([k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where]) > 0:
                            i[1] = [[k for k in [[i[0][l] + houi[houi.index(i[1]) - 1 + j][l] for l in range(2)] for j in range(4)] if k in xs[0] and not k in where][0][s] - i[0][s] for s in range(2)]
                    go_point = [[i[0][l] + i[1][l] for l in range(2)] for i in xs[1]]
                    for i in range(len(go_point)):
                        if go_point[i] in where or not go_point[i] in xs[0]:
                            go_point[i] = xs[1][i][0]
                        else:
                            if go_point.count(go_point[i]) > 1:
                                for j in range(4):
                                    if xs[1][i] == [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]]:
                                        break
                                    elif [[go_point[i][l] + houi[j][l] for l in range(2)] ,houi[j + 2]] in xs[1]:
                                        go_point[i] = xs[1][i][0]
                                        break
                    else:
                        xs[1] = [k for k in [[go_point[i] ,xs[1][i][1]] for i in range(len(xs[1]))] if not k[0] in xs[2]]
            else:
                ans.append("NA")
hinan()