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 sys import stdin
readline = stdin.readline


from itertools import product

N, W, S, E = (0, -1), (-1, 0), (0, 1), (1, 0)
BLANK = ('.', 'X')
DIRECTION = {
    'N': N,
    'W': W,
    'S': S,
    'E': E,
}
check_priority = {
    N: (E, N, W, S),
    W: (N, W, S, E),
    S: (W, S, E, N),
    E: (S, E, N, W),
    }
step_priority = (E, N, W, S)


def search_people(sq):
    people = []
    for y, x in product(range(h), range(w)):
        if sq[y][x] in DIRECTION:
            sq[y][x] = person = Person(x, y, DIRECTION[sq[y][x]], sq)
            people.append(person)
    return people


class Person:
    def __init__(self, x, y, direction, sq):
        self.x, self.y = x, y
        self.direction = direction
        self.sq = sq
        self.evacuating = True

    def check_direction(self):
        for direction in check_priority[self.direction]:
            dx, dy = direction
            if self.sq[self.y + dy][self.x + dx] in BLANK:
                self.direction = direction
                break

    def check_stepable(self):
        dx, dy = self.direction
        x, y = self.x + dx, self.y + dy
        if self.sq[y][x] == 'X':
            self.stepable = True
            return
        if self.sq[y][x] != '.':
            self.stepable = False
            return
        for dx, dy in step_priority:
            tmp = self.sq[y + dy][x + dx]
            if not isinstance(tmp, Person):
                continue
            if tmp == self:
                self.stepable = True
                break
            dx, dy = tmp.direction
            if tmp.x + dx == x and tmp.y + dy == y:
                self.stepable = False
                break

    def __repr__(self):
        return "'P'"

    def step(self):
        if self.stepable:
            dx, dy = self.direction
            self.sq[self.y][self.x] = '.'
            self.x += dx
            self.y += dy
            if self.sq[self.y][self.x] == '.':
                self.sq[self.y][self.x] = self
            else:
                self.evacuating = False


while True:
    w, h = map(int, readline().split())
    if w == 0:
        break
    squares = [list(readline().strip()) for _ in range(h)]
    people = search_people(squares)
    for i in range(180):
        for pi in people:
            pi.check_direction()
        for pi in people:
            pi.check_stepable()
        for pi in people:
            pi.step()
        people = [pi for pi in people if pi.evacuating]
        if len(people) == 0:
            print(i + 1)
            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]])
        dest = sorted(dest,key=lambda x: (x[2:],x[1]))
        # print(dest)
        # 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]:
                # print("move",W,H,ps[pi][:2],dy,dx)
                mt[dy][dx] |= 1
                ps[pi][:2] = [dy,dx]
            if m[ps[pi][0]][ps[pi][1]] & 16:
                # print("OUT")
                # print(ttt)
                # time.sleep(3)
                dellist.append(pi)
        # print(dellist)
        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()