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>
using namespace std;
#define inRange(x,a,b) (a <= x && x < b)

int di[4] = {-1, 0, 1, 0};
int dj[4] = { 0, 1, 0,-1};

int d[4] = {1, 0, -1, 2};

int main(){
    int w, h;
    while(cin >> w >> h, w+h){
        char mat[h][w];
        for(int i = 0; i < h*w; i++)    cin >> mat[i/w][i%w];
        int people = 0;
        vector<vector<int>> v(h, vector<int>(w, -1));
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                if(mat[i][j]=='N')  people++, mat[i][j]='.', v[i][j] = 0;
                if(mat[i][j]=='E')  people++, mat[i][j]='.', v[i][j] = 1;
                if(mat[i][j]=='S')  people++, mat[i][j]='.', v[i][j] = 2;
                if(mat[i][j]=='W')  people++, mat[i][j]='.', v[i][j] = 3;
            }
        }
        int tim = 0;
        while(++tim <= 180){
            vector<vector<int>> next(h, vector<int>(w, -1));

            // change direction
            for(int i = 0; i < h; i++){
                for(int j = 0; j < w; j++){
                    if(v[i][j] == -1)   continue;
                    for(int k = 0; k < 4; k++){
                        int dir = (v[i][j]+d[k]+4)%4;   // change direction(try)
                        int ni = i+di[dir], nj = j+dj[dir]; // move forward(try)
                        if(inRange(ni,0,h)&&inRange(nj,0,w)&&v[ni][nj]==-1&&mat[ni][nj]!='#'){
                            v[i][j] = dir;  // change direction(decide!)
                            break;
                        }
                    }
                }
            }
            
            // move
            for(int k = 3; k >= 0; k--){
                for(int i = 0; i < h; i++){
                    for(int j = 0; j < w; j++){
                        if(v[i][j] != k)    continue;
                        int ni = i + di[k], nj = j + dj[k];
                        if(inRange(ni,0,h)&&inRange(nj,0,w)&&mat[ni][nj]!='#'&&v[ni][nj]==-1&&next[ni][nj]==-1){
                            next[ni][nj] = v[i][j];
                            v[i][j] = -2;
                            continue;
                        }
                    }
                }
            }

            // move(stupid)
            for(int i = 0; i < h; i++){
                for(int j = 0; j < w; j++){
                    if(v[i][j] != -1 && v[i][j] != -2)   next[i][j] = v[i][j];
                }
            }
            
            // vanish
            for(int i = 0; i < h; i++){
                for(int j = 0; j < w; j++){
                    if(mat[i][j]=='X'&&next[i][j]!=-1){
                        next[i][j] = -1;
                        people--;
                    }
                }
            }
            
            v = next;
            if(people == 0) break;
        }

        if(tim > 180)   cout << "NA" << endl;
        else            cout << tim << endl;
    }
    return 0;
}