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 <cstring>

using namespace std;

class person{
public:
    int x;
    int y;
    int dir;
    bool exit;
    person(int x, int y, int dir, int exit):
        x(x),y(y),dir(dir),exit(exit){}
};

//priority:(person-dir,next-dir)
//south-right
//east-right
//north-right
//west-right
//west-front
//south-front
//east-front
//north-front
//north-left
//west-left
//south-left
//east-left
//east-back
//north-back
//west-back
//south-back
int w,h;
int f[32][32];
int dx[4] = {1,0,-1,0}; //E,N,W,S
int dy[4] = {0,-1,0,1};
int dir[16] = { 3, 0, 1, 2, 2, 3, 0, 1, 1, 2, 3, 0, 0, 1, 2, 3};
int ndir[16] ={ 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1};

void pr(){
    for(int i=1;i<=h;i++){
        for(int j=1;j<=w;j++){
            if(f[i][j] == -1){
                cout << "#";
            }else if(f[i][j] == 0){
                cout << ".";
            }else if(f[i][j] == 1){
                cout << "X";
            }else{
                cout << f[i][j]-2;
            }
        }
        cout << endl;
    }
}

int main(){
    while(cin >> w >> h){
        if((w|h) == 0) break;
        char tmp;
        vector<person> ps;
        memset(f, -1,sizeof(f));

        int cnt = 2;
        for(int i=1;i<=h;i++){
            for(int j=1;j<=w;j++){
                cin >> tmp;
                if(tmp == '#'){
                    f[i][j] = -1;
                }else if(tmp == 'X'){
                    f[i][j] = 1;
                }else if(tmp == '.'){
                    f[i][j] = 0;
                }else if(tmp == 'E'){
                    ps.push_back(person(j,i,0,false));
                    f[i][j] = cnt++;
                }else if(tmp == 'N'){
                    ps.push_back(person(j,i,1,false));
                    f[i][j] = cnt++;
                }else if(tmp == 'W'){
                    ps.push_back(person(j,i,2,false));
                    f[i][j] = cnt++;
                }else{
                    ps.push_back(person(j,i,3,false));
                    f[i][j] = cnt++;
                }
            }
        }

        int res=0;
        bool isended;
        bool moved[(int)ps.size()];
        vector<pair<int,int> > inst;
        for(;;){
            memset(moved, false, sizeof(moved));
            //pr();
            if(res > 180) break;
            isended = true;
            for(int j=0;j<16;j++){
                for(int i=0;i<(int)ps.size();i++){
                    if(ps[i].exit == false){
                        isended = false;
                        if(ps[i].dir == dir[j] && moved[i] == false){
                            int nx = ps[i].x + dx[ndir[j]];
                            int ny = ps[i].y + dy[ndir[j]];
                            if(f[ny][nx] == -1) continue;
                            //cout << i << ":" << ny << " " << nx << " " << f[ny][nx] << endl;
                            if(f[ny][nx] == 1){
                                //cout << "evac" << endl;
                                ps[i].exit = true;
                                f[ps[i].y][ps[i].x] = -2;
                                moved[i] = true;
                                inst.push_back(make_pair(ps[i].y,ps[i].x));
                            }else if(f[ny][nx] == 0){
                                //cout << "move" << endl;
                                f[ps[i].y][ps[i].x] = -2;
                                inst.push_back(make_pair(ps[i].y,ps[i].x));
                                ps[i].x = nx;
                                ps[i].y = ny;
                                ps[i].dir = ndir[j];
                                f[ny][nx] = i+2;
                                //cout << ny << " " << nx << endl;
                                moved[i] = true;                         
                            }else if(f[ny][nx] >= 2 && moved[f[ny][nx]-2] == true){
                                //cout << moved[f[ny][nx]-2] << endl;
                                moved[i] = true;
                            }
                        }
                    }
                }
            }
            for(int i=0;i<(int)inst.size();i++){
                f[inst[i].first][inst[i].second] = 0;
            }
            inst.clear();
            //int hoge;
            //cin >> hoge;
            if(isended) break;
            res++;
        }
        if(res > 180){
            cout << "NA" << endl;
        }else{
            cout << res << endl;
        }
    }
}