Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

type Point struct {
	x, y int
}

func main() {
	dx := []int{1, 0, -1, 0}
	dy := []int{0, -1, 0, 1}
	maze := make([][]rune, 30)
	for i := range maze {
		maze[i] = make([]rune, 30)
	}
	next := make([][]rune, 30)
	for i := range next {
		next[i] = make([]rune, 30)
	}
	check := make([][]int, 30)
	for i := range check {
		check[i] = make([]int, 30)
	}
	for scanner.Scan() {
		buf := strings.Split(scanner.Text(), " ")
		W, _ := strconv.Atoi(buf[0])
		H, _ := strconv.Atoi(buf[1])
		if W == 0 && H == 0 {
			break
		}
		for i := 0; i < H; i++ {
			scanner.Scan()
			buf_ := scanner.Text()
			for j, v := range buf_ {
				maze[i][j] = v
			}
		}
		man := 0
		for i := 0; i < H; i++ {
			for j := 0; j < H; j++ {
				if maze[i][j] != '.' && maze[i][j] != 'X' && maze[i][j] != '#' {
					switch maze[i][j] {
					case 'E':
						maze[i][j] = 0
						man++
					case 'N':
						maze[i][j] = 1
						man++
					case 'W':
						maze[i][j] = 2
						man++
					case 'S':
						maze[i][j] = 3
						man++
					}
				}
			}
		}
		ans := -1
		for t := 0; t <= 180; t++ {
			if man == 0 {
				ans = t
				break
			}
			for i := 0; i < H; i++ {
				for j := 0; j < W; j++ {
					check[i][j] = 0x7FFFFFFF
				}
			}
			for i := 0; i < H; i++ {
				for j := 0; j < W; j++ {
					if maze[i][j] > 3 {
						continue
					}
					l := (maze[i][j] + 3) % 4
					for k := 0; k < 4; k++ {
						y := i + dy[l]
						x := j + dx[l]
						if 0 <= y && y < H && 0 <= x && x < W && (maze[y][x] == '.' || maze[y][x] == 'X') {
							maze[i][j] = l
							check[y][x] = min(check[y][x], int((l+2)%4))
							break
						}
						l = (l + 1) % 4
					}
				}
			}
			for i := 0; i < H; i++ {
				for j := 0; j < W; j++ {
					next[i][j] = maze[i][j]
				}
			}
			for i := 0; i < H; i++ {
				for j := 0; j < W; j++ {
					if maze[i][j] > 3 {
						continue
					}
					y := i + dy[maze[i][j]]
					x := j + dx[maze[i][j]]
					if 0 <= y && y < H && 0 <= x && x < W && check[y][x] == int((maze[i][j]+2)%4) {
						if maze[y][x] == '.' {
							next[y][x] = maze[i][j]
						} else if maze[y][x] == 'X' {
							man--
						}
						next[i][j] = '.'
					}
				}
			}
			for i := 0; i < H; i++ {
				for j := 0; j < W; j++ {
					maze[i][j]=next[i][j]
				}
			}
		}
		if ^ans != 0 {
			fmt.Println(ans)
		} else {
			fmt.Println("NA")
		}
	}
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

Python: from collections import defaultdict
from typing import Dict


class Players(object):
    def __init__(self):
        self.players: Dict[int, "Player"] = {}
        self.reservation = defaultdict(list)
        self.square = None
        self.goal = []

    def set_goal(self, x, y):
        self.goal.append((x, y))

    def add(self, player: "Player"):
        self.players[len(self.players)] = player

    def search_all(self):
        for i, player in self.players.items():
            x, y, d = player.search(self.square)
            self.reservation[(x, y)].append(((d-2)%4, i))
        for k, v in self.reservation.items():
            if len(v) == 1:
                continue
            self.reservation[k] = [min(v)]

    def move_all(self):
        for (x, y), l in self.reservation.items():
            _id = l[0][1]
            _x, _y = self.players[_id].pos
            self.players[_id].move(x, y)
            self.square[_y][_x], self.square[y][x] = ".", "o"

        self.reservation.clear()
        for i, player in self.players.copy().items():
            x, y = player.pos
            if (x, y) in self.goal:
                del self.players[i]
                self.square[y][x] = "."

    @property
    def is_finished(self):
        return len(self.players) == 0


class Player(object):
    dir_dict = {"E": 0, "N": 1, "W": 2, "S": 3}
    dir_diff = ((1, 0), (0, -1), (-1, 0), (0, 1))

    def __init__(self, x, y, direction):
        self.x = x
        self.y = y
        self.dir = self.dir_dict[direction]

    def search(self, square):
        for i in range(self.dir-1, self.dir+3):
            dx, dy = self.dir_diff[i % 4]
            if square[self.y+dy][self.x+dx] in (".", "X"):
                self.dir = i % 4
                return self.x+dx, self.y+dy, self.dir
        else:
            return self.x, self.y, self.dir

    def move(self, x, y):
        self.x, self.y = x, y

    @property
    def pos(self):
        return self.x, self.y


while True:
    W, H = map(int, input().split())
    if W == 0:
        break
    square = []
    players = Players()
    for y in range(H):
        square.append(list(input()))
        for x, c in enumerate(square[-1]):
            if c in "ENWS":
                players.add(Player(x, y, c))
            elif c == "X":
                players.set_goal(x, y)
    players.square = square

    for i in range(1, 121):
        players.search_all()
        players.move_all()
        if players.is_finished:
            print(i)
            break
    else:
        print("NA")

Python: from sys import stdin
from itertools import chain
head = [[8,1,2,4],[1,2,4,8],[2,4,8,1],[4,8,1,2]]

while(True):
    W,H = map(int, stdin.readline().split())
    m = [[0]*W for _ in range(H)]
    ps = []
    if not (W or H): break
    flag = True
    for h in range(H):
        s = stdin.readline()
        if "X" in s: flag = False
        for w in range(W):
            if s[w] in [".", "X", "E", "N", "W", "S"]:
                m[h][max(0,w-1)] |= 1
                m[min(H-1,h+1)][w] |= 2
                m[h][min(W-1,w+1)] |= 4
                m[max(0,h-1)][w] |= 8
            if s[w] in ["E", "N", "W", "S"]:
                ps.append([h,w,["E", "N", "W", "S"].index(s[w])])
            if s[w] == "X":
                m[h][w] |= 16
    if flag:
        print("NA")
        continue
    if W <= 2 or H <= 2:
        print(1)
        continue
    # print(ps)
    # for n in m:
    #     print(n)
    ttt = 0
    while(True):
        ttt += 1
        if ttt > 180: print("NA"); break
        # import time
        # time.sleep(1)
        mt = [[0]*W for _ in range(H)]
        for p in ps:
            mt[p[0]][p[1]] |= 1
        dest = []
        # for mmm in mt:
        #     print(mmm)
        # print()
        # print([a[:2] for a in ps])
        for pi,p in enumerate(ps):
            for i in range(4):
                if head[p[2]][i] & m[p[0]][p[1]]:
                    d = head[p[2]][i]
                    if   d == 1 and not [p[0],p[1]+1] in [a[:2] for a in ps]: p[2] = 0; dest.append([pi,2,p[0],p[1]+1]); break
                    elif d == 2 and not [p[0]-1,p[1]] in [a[:2] for a in ps]: p[2] = 1; dest.append([pi,3,p[0]-1,p[1]]); break
                    elif d == 4 and not [p[0],p[1]-1] in [a[:2] for a in ps]: p[2] = 2; dest.append([pi,0,p[0],p[1]-1]); break
                    elif d == 8 and not [p[0]+1,p[1]] in [a[:2] for a in ps]: p[2] = 3; dest.append([pi,1,p[0]+1,p[1]]); break
            else:
                dest.append([pi,(p[2]+2)&3,p[0],p[1]])
        # print(dest)
        dest = sorted(dest,key=lambda x: (x[2:],x[1]))
        # for mmm in mt:
        #     print(mmm)
        # print("ps = ",ps,dest)
        dellist = []
        for pi,d,dy,dx in dest:
            # print(W,H,dy,dx,mt)
            if not mt[dy][dx]:
                mt[dy][dx] |= 1
                ps[pi][:2] = [dy,dx]
            if m[dy][dx] & 16:
                # print("OUT")
                # print(ttt)
                # time.sleep(3)
                dellist.append(pi)
        for idx in sorted(dellist)[::-1]:
            del ps[idx]
        if not len(ps):
            print(ttt)
            break

    # ms = [stdin.readline() for _ in range(H)]
    # print(m)
    # from pprint import pprint
    # print(ps)
    # for n in m:
    #     print(n)
    # print()