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
}

C++: #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>

enum class Direction {
	East, North, West, South
};
enum class RelationalDirection {
	Right, Forward, Left, Backward
};
constexpr std::array<Direction, 4> Directions{ Direction::East, Direction::North, Direction::West, Direction::South };
constexpr std::array<RelationalDirection, 4> RelationalDirestions{ RelationalDirection::Right, RelationalDirection::Forward, RelationalDirection::Left, RelationalDirection::Backward };

int to_int(Direction dir) {
	switch (dir) {
	case Direction::East: return 0;
	case Direction::North: return 1;
	case Direction::West: return 2;
	case Direction::South: return 3;
	default: throw 0;
	}
}
int to_int(RelationalDirection dir) {
	switch (dir) {
	case RelationalDirection::Right: return 3;
	case RelationalDirection::Forward: return 0;
	case RelationalDirection::Left: return 1;
	case RelationalDirection::Backward: return 2;
	default: throw 0;
	}
}

Direction operator+(Direction dir, RelationalDirection rdir) {
	return Directions[(to_int(dir) + to_int(rdir)) % 4];
}
Direction operator+(RelationalDirection rdir, Direction dir) {
	return Directions[(to_int(rdir) + to_int(dir)) % 4];
}

struct Coordinate {
	int x, y;
};
Coordinate from_dir(Direction dir) {
	switch (dir) {
	case Direction::East: return Coordinate{ 1, 0 };
	case Direction::North: return Coordinate{ 0, -1 };
	case Direction::West: return Coordinate{ -1, 0 };
	case Direction::South: return Coordinate{ 0, 1 };
	default: throw 0;
	}
}
Coordinate operator+(Coordinate a, Coordinate b) {
	return Coordinate{ a.x + b.x, a.y + b.y };
}
Coordinate operator+(Coordinate a, Direction dir) {
	return a + from_dir(dir);
}
Coordinate operator+(Direction dir, Coordinate a) {
	return from_dir(dir) + a;
}
bool operator==(Coordinate a, Coordinate b) {
	return a.x == b.x && a.y == b.y;
}

struct Person {
	Coordinate position;
	Direction dir;
};

int main() {
	while (true) {
		int width, height; std::cin >> width >> height; if (width == 0 && height == 0) break;
		std::vector<std::string> state(height); for (auto& line : state) std::cin >> line;
		std::vector<Person> person;
		for (auto i = 0; i < height; ++i) for (auto j = 0; j < width; ++j) {
			switch (state[i][j]) {
			case 'E': person.push_back(Person{ Coordinate{j, i}, Direction::East }); break;
			case 'N': person.push_back(Person{ Coordinate{j, i}, Direction::North }); break;
			case 'W': person.push_back(Person{ Coordinate{j, i}, Direction::West }); break;
			case 'S': person.push_back(Person{ Coordinate{j, i}, Direction::South }); break;
			default: break;
			}
		}
		std::vector<Coordinate> next_pos(person.size());
		std::vector<std::vector<int>> id(height, std::vector<int>(width, -1));
		std::vector<std::vector<bool>> is_in_queue(height, std::vector<bool>(width, false));
		std::queue<int> in_maze; for (auto i = 0; i < person.size(); ++i) {
			in_maze.push(i);
			id[person[i].position.y][person[i].position.x] = i;
		}
		std::queue<Coordinate> move_target;
		int count = -1;
		for (; count <= 180 && !in_maze.empty(); ++count) {
			for (auto c = in_maze.size(); c > 0; --c) {
				auto top = in_maze.front(); in_maze.pop();
				if (state[person[top].position.y][person[top].position.x] == 'X') continue;
				for (const auto dir : RelationalDirestions) {
					auto next = person[top].position + (person[top].dir + dir);
					if (state[next.y][next.x] != '#' && id[next.y][next.x] == -1) {
						if (!is_in_queue[next.y][next.x]) {
							is_in_queue[next.y][next.x] = true;
							move_target.push(next);
						}
						person[top].dir = person[top].dir + dir;
						next_pos[top] = next;
						break;
					}
				}
				in_maze.push(top);
			}
			while (!move_target.empty()) {
				auto top = move_target.front(); move_target.pop();
				is_in_queue[top.y][top.x] = false;
				for (const auto dir : Directions) {
					auto suspect = top + dir;
					if (0 <= suspect.y && suspect.y < height && 0 <= suspect.x && suspect.x < width && id[suspect.y][suspect.x] != -1 && next_pos[id[suspect.y][suspect.x]] == top) {
						if (state[top.y][top.x] != 'X') {
							id[top.y][top.x] = id[suspect.y][suspect.x];
						}
						person[id[suspect.y][suspect.x]].position = top;
						id[suspect.y][suspect.x] = -1;
						break;
					}
				}
			}
		}
		if (count > 180) {
			std::cout << "NA\n";
		}
		else {
			std::cout << count << '\n';
		}
	}
}