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 <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

char s[] = "ENWS";
int rev[128];
int dy[] = {0, -1, 0, 1};
int dx[] = {1, 0, -1, 0};

struct Person {
	int y, x;
	char dir;
	int candy, candx;
	Person(int y, int x, char dir) : y(y), x(x), dir(dir){}
	Person(){}
};

char map[32][32];

void moveIt(vector<Person> &v, int num, int i, int j)
{
	if (map[i][j] == 'X'){
		map[v[num].y][v[num].x] = '.';
		v.erase(v.begin() + num, v.begin() + num + 1);
	}
	else {
		map[v[num].y][v[num].x] = '.';
		v[num].y = v[num].candy; v[num].x = v[num].candx;
		map[v[num].y][v[num].x] = v[num].dir;
	}
}

int main()
{
	int w, h;
	
	for (int i = 0; i < 4; i++) rev[s[i]] = i;
	while (scanf("%d %d", &w, &h) && w){
		vector<Person> v;
		
		for (int i = 0; i < h; i++){
			scanf("%s", map[i]);
			for (int j = 0; j < w; j++){
				if (map[i][j] != 'X' && map[i][j] != '#' && map[i][j] != '.'){
					v.push_back(Person(i, j, map[i][j]));
				}
			}
		}
		
		int time = 0;
		int count[32][32];
		while (time <= 180){
			if (v.size() == 0){
				printf("%d\n", time);
				goto next;
			}
			
			for (int i = 0; i < h; i++)
				for (int j = 0; j < w; j++)
					count[i][j] = 0;
			
			for (int num = 0; num < v.size(); num++){
				v[num].candy = v[num].candx = -1;
				for (int i = 0; i < 4; i++){
					int ndir = (rev[v[num].dir] + 3 + i) % 4;
					int ny = v[num].y + dy[ndir], nx = v[num].x + dx[ndir];
					if (map[ny][nx] == '.' || map[ny][nx] == 'X'){
						v[num].candy = ny; v[num].candx = nx;
						v[num].dir = s[ndir];
						count[v[num].candy][v[num].candx]++;
						break;
					}
				}
			}
			
			for (int i = 0; i < h; i++){
				for (int j = 0; j < w; j++){
					if (count[i][j] == 1){
						for (int num = 0; num < v.size(); num++){
							if (v[num].candy != i || v[num].candx != j) continue;
							moveIt(v, num, i, j);
						}
					}
					else if (count[i][j] > 1){
						int pos = -1, priority = 4;
						for (int num = 0; num < v.size(); num++){
							if (v[num].candy != i || v[num].candx != j) continue;
							int p = rev[v[num].dir];
							p = p < 2 ? (p + 2) : (p - 2);
							if (p < priority){
								pos = num; priority = p;
							}
						}
						
						moveIt(v, pos, i, j);
					}
				}
			}
			time++;
		}
		
		printf("NA\n");
		next:;
	}
	
	return (0);
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <complex>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <iterator>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define iter(c) __typeof__((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INFTY=1<<29;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

void solve(vs& grid)
{
	int h=grid.size(),w=grid[0].size();
	char dir[]="ENWS";
	int di[]={0,-1,0,1},dj[]={1,0,-1,0};
	
	for(int t=0;t<=180;t++){
		bool escaped=true;
		rep(i,h) rep(j,w)
			escaped&=!strchr(dir,grid[i][j]);
		if(escaped){
			cout<<t<<endl;
			return;
		}
		
		vvi flg(h,vi(w));
		rep(i,h) rep(j,w) if(strchr(dir,grid[i][j])){
			int cd=strchr(dir,grid[i][j])-dir;
			rep(k,4){
				int nd=(cd+3+k)%4;
				int ni=i+di[nd],nj=j+dj[nd];
				if(ni<0 || h<=ni || nj<0 || w<=nj || !(grid[ni][nj]=='.' || grid[ni][nj]=='X'))
					continue;
				grid[i][j]=dir[nd];
				flg[ni][nj]=1;
				break;
			}
		}
		
		rep(i,h) rep(j,w) if(flg[i][j]){
			rep(k,4){
				int pi=i+di[k],pj=j+dj[k];
				if(pi<0 || h<=pi || pj<0 || w<=pj || grid[pi][pj]!=dir[(k+2)%4])
					continue;
				if(grid[i][j]=='.')
					grid[i][j]=grid[pi][pj];
				grid[pi][pj]='.';
				break;
			}
		}
	}
	cout<<"NA"<<endl;
}

int main()
{
	for(int w,h;cin>>w>>h,w|h;){
		vs grid(h);
		rep(i,h) cin>>grid[i];
		
		solve(grid);
	}
	
	return 0;
}