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;
			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);

				if (s == 0) m[v[i].p.imag()][v[i].p.real()] = 2;
				else v.erase( v.begin() + i-- );
			}
			for (int i = 0; i < zero.size(); ++i)
				m[zero[i].imag()][zero[i].real()] = 0;
		}

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

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF (1<<29)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))
#define fr first
#define sc second
typedef long long int ll;
//const int dx[] = {1,0,-1,0},dy[] = {0,-1,0,1};
const int dx[] = {-1,0,1,0},dy[] = {0,1,0,-1};
const int ddir[] = {3,0,1,2};

//const char dir[] = {'E','N','W','S'};
const char dir[] = {'W','S','E','N'};


using namespace std;

struct person{
    int dir;
    int x;
    int y;
};

void Sort(vector< struct person> &p){
    vector< pair< int,pair<int,int> > > ret;
    REP(i,p.size()){
	ret.PB(pair<int,pair<int,int> >(p[i].dir,pair<int,int>(p[i].x,p[i].y)));
    }
    sort(RALL(ret));
    REP(i,p.size()){
	p[i].dir = ret[i].fr;
	p[i].x = ret[i].sc.fr;
	p[i].y = ret[i].sc.sc;
    }
}

int main(){
    while(true){
	int w,h;
	cin >> w >> h;
	if(w == 0 && h == 0) break;
	vector< vector<char> > board(h,vector<char>(w));
	vector< struct person > people;
	REP(i,h)REP(j,w){
	    cin >> board[i][j];
	    bool flg = false;
	    REP(k,4){
		if(dir[k] == board[i][j]) flg = true;
	    }
	    if(flg){
		struct person p;
		p.x = j;
		p.y = i;
		REP(k,4){
		    if(board[i][j] == dir[k]){
			p.dir = k;
			break;
		    }
		}
		people.PB(p);
	    }
	}
	REP(a,181){
	    vector< vector<char> > board1 = board;
	    REP(i,people.size()){
		int x = people[i].x;
		int y = people[i].y;
		board1[y][x] = '.';
		/*
		cout << "board1[y][x] = "<< board1[y][x] << endl;
		REP(i,h){
		    REP(j,w){
			cout << board1[i][j];
		    }
		    cout << endl;
		}
		cout << "board[y][x] = "<< board[y][x] << endl;
		REP(i,h){
		    REP(j,w){
			cout << board[i][j];
		    }
		    cout << endl;
		}
		*/
	    }
	    RREP(i,people.size()){
		REP(k,4){
		    int nk = (ddir[k]+people[i].dir)%4;
		    int nx = people[i].x + dx[nk];
		    int ny = people[i].y + dy[nk];
		    if(nx<0||w<=nx||ny<0||h<=ny) continue;
		    if(board[ny][nx] != '#'){
			/*
			cout << "===BEGIN===" << endl;
			printf("people.x = %d , people.y = %d\n",people[i].x,people[i].y);
			printf("board[%d][%d] = %c\n",ny,nx,board[ny][nx]);
			printf("nx = %d ny = %d\n",nx,ny);
			printf("dir = %d\n",people[i].dir);
			printf("nk = %d dx = %d dy = %d\n",nk,dx[nk],dy[nk]);
			cout << "===END===" << endl;
			*/
			bool leftflg = false;
			REP(j,4){
			    if(board[ny][nx] == dir[j]) leftflg =true;
			}
			if(leftflg) {
			    continue;
			}
			people[i].dir = nk;
			break;
		    }
		}
	    }
	    //sort(RALL(people));
	    Sort(people);
	    bool flg = false;
	    RREP(i,people.size()){
		int nk = people[i].dir;
		int nx = people[i].x + dx[nk];
		int ny = people[i].y + dy[nk];
		//printf("nx = %d ny = %d\n",nx,ny);
		if(board1[ny][nx] != '.'){
		    if(!flg && board1[ny][nx] == 'X'){
			//flg = true;
			people.erase(people.begin()+i);
			continue;
		    }
		    nx = people[i].x;
		    ny = people[i].y;
		}
		
		people[i].x = nx;
		people[i].y = ny;
		board1[ny][nx] = dir[people[i].dir];
	    }
	    board = board1;
	    /*
	    cout << "board1==== "<<endl;
	    REP(i,h){
		REP(j,w){
		    cout << board[i][j];
		}
		cout << endl;
	    }
	    */
	    if(people.size()==0){
		cout << a+1 << endl;
		break;
	    }
	    if(a == 180){
		cout << "NA" << endl;
	    }
	}
    }
    return 0;
}