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
        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
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':
            return True
        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')