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 <algorithm>
#include <complex>

using namespace std;

typedef complex<int> P;

struct human {
	P p;
	int dir;

	human(P p, int dir) : p(p), dir(dir) { }
	human() { this -> p = P(0, 0); this -> dir = 0; }
};

bool cp(human a, human b) {
	return a.dir > b.dir;
}

P dir(int n) {
	n %= 4;
	return n == 0 ? P(0, -1) : n == 1 ? P(1, 0) : n == 2 ? P(0, 1) : P(-1, 0);
}

int W, H;
int m[30][30];
int state(P p) {
//	if (p.real() < 0 || p.real() >= W || p.imag() < 0 || p.imag() >= H) cout << "ERROR" << endl;
//	cout << p.real() << " " << p.imag() << endl;
	return m[p.imag()][p.real()];
}

int main() {
	while (cin >> W >> H, W || H) {
		vector<human> v;

		for (int y = 0; y < H; ++y) {
			string str; cin >> str;
			for (int x = 0; x < W; ++x)
				m[y][x] =
					  str[x] == '#' ? 1
					: str[x] == 'X' ? -1
					: str[x] == 'N' ? (v.push_back( human( P(x, y), 0) ), 2)
					: str[x] == 'E' ? (v.push_back( human( P(x, y), 1) ), 2)
					: str[x] == 'S' ? (v.push_back( human( P(x, y), 2) ), 2)
					: str[x] == 'W' ? (v.push_back( human( P(x, y), 3) ), 2)
					: 0
					;
		}

		int t = 0;
		while (v.size() && ++t <= 180) {
			// 向き変更
			for (int i = 0; i < v.size(); ++i)
				v[i].dir =
					  state(v[i].p + dir(v[i].dir + 1)) <= 0 ? (v[i].dir + 1) % 4
					: state(v[i].p + dir(v[i].dir))     <= 0 ? v[i].dir
					: state(v[i].p + dir(v[i].dir + 3)) <= 0 ? (v[i].dir + 3) % 4
					: state(v[i].p + dir(v[i].dir + 2)) <= 0 ? (v[i].dir + 2) % 4
					: v[i].dir
					;

			// 向きでソート
			sort( v.begin(), v.end(), cp );

			// 移動
			vector<P> zero;
			vector<P> negative;
			for (int i = 0; i < v.size(); ++i) {
				int s = state(v[i].p + dir(v[i].dir));
				if (s > 0) continue;

				zero.push_back(v[i].p);
				v[i].p += dir(v[i].dir);

				m[v[i].p.imag()][v[i].p.real()] = 2;
				if (s < 0) {
					negative.push_back(v[i].p);
					v.erase( v.begin() + i-- );
				}
			}

			for (int i = 0; i < zero.size(); ++i)
				m[zero[i].imag()][zero[i].real()] = 0;

			for (int i = 0; i < negative.size(); ++i)
				m[negative[i].imag()][negative[i].real()] = -1;
		}

		if (t > 180) cout << "NA" << endl;
		else cout << t << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
using namespace std;

int w, h;

// 0:→, 1:↑, 2:←, 3:↓
int dx[4] = {+1,0,-1,0};
int dy[4] = {0,-1,0,+1};

void debug(const vector<string>& s, int t){
	char H[256] = {0};
	H['E'] = '>'; H['W'] = '<'; H['N'] = '^'; H['S'] = 'v';
	H['X'] = 'X'; H['.'] = '.'; H['#'] = '#';
	
	cout << "time : " << t << endl;
	for(int y=0 ; y < h ; y++ ){
		for(int x=0 ; x < w ; x++ ){
			cout << H[s[y][x] ];
		}
		cout << endl;
	}
	cout << endl;
}

// 位置 (px,py), 向き d, の人が空いているマスを探し, 最初に見つけた向きを返す.
// ないときは d を返す. 
int search(const vector<string>& s, int px, int py, int d){
	// 右, 前, 左, 後ろの順に調べる.
	int f[4] = {+3,0,+1,+2};
	
	for(int i=0 ; i < 4 ; i++ ){
		int next_d = (d + f[i]) % 4;
		int mx = px + dx[next_d];
		int my = py + dy[next_d];
		
		if( mx < 0 || my < 0 || mx >= w || my >= h ) continue;
		 
		if( s[my][mx] == '.' || s[my][mx] == 'X' ){
			return next_d;
		}
	}
	return d;
}

bool is_person(char c){
	return ( c == 'E' || c == 'W' || c == 'N' || c == 'S');
}

int main(){
	char H[256] = {0};
	H['E'] = 0; H['N'] = 1; H['W'] = 2; H['S'] = 3;
	H[0] = 'E'; H[1] = 'N'; H[2] = 'W'; H[3] = 'S';
	
	while( cin >> w >> h, w || h ){
		vector<string> s(h);
		for(int y=0 ; y < h ; y++ ){
			cin >> s[y];
		}
		//debug( s , 0 );
		// N := 人の数
		int N=0;
		// 人の数を数える.
		for(int y=0 ; y < h ; y++ ){
			for(int x=0 ; x < w ; x++ ){
				if( is_person(s[y][x]) ) N++;
			}
		}
		int ans = -1;
		for(int t=1 ; t <= 180 ; t++ ){
			for(int y=0 ; y < h ; y++ ){
				for(int x=0 ; x < w ; x++ ){
					// (x,y)が人だったら向きを変える
					if( is_person(s[y][x]) ){
						int d = H[ s[y][x] ];
						int next_d = search( s , x , y , d );
						s[y][x] = H[next_d];
					}
				}
			}
			
			vector<string> s_ = s;
			bool cp[100][100] = {false};
			for(int y=0 ; y < h ; y++ ){
				for(int x=0 ; x < w ; x++ ){
					// (x,y) が移動可能なマス かつ まだ誰も移動していなければ調べる.
					if( s[y][x] == '.' || s[y][x] == 'X'){
						// 優先順位は マス(x,y) の右,上,左,下 の順.
						for(int i=0 ; i < 4 ; i++ ){
							int mx = x + dx[i];
							int my = y + dy[i];
							
							if( mx < 0 || my < 0 || mx >= w || my >= h ) continue;
							
							// (mx,my) にいる人が (x,y) に移動する. (x,y) が出口'X' なら人は消える.
							if( is_person(s[my][mx]) && abs(i-H[s[my][mx]]) == 2 ){
								if( s[y][x] == 'X' ){
									s_[my][mx] = '.';
									cp[my][mx] = true;
									N--;
								}else{
									s_[my][mx] = '.';
									s_[y][x] = s[my][mx];
									cp[y][x] = true;
									cp[my][mx] = true;
								}
								break;
							}
						}
					}
					if( !cp[y][x] ) s_[y][x] = s[y][x];
				}
			}
			s = s_;
			//debug( s , t );
			if( N == 0 ){
				ans  = t;
				break;
			}
		}
		if( ans == -1 ){
			cout << "NA" << endl;
		}else{
			cout << ans << endl;
		}
	}
}