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 <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <cstring>
#include <cctype>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <complex>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(i,a,b) for(int i = a; i < (int)b; i++)
const int INF = 1<<28;

int W, H, ppl;
char m[30][30];
string d = "WSEN";
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};

bool canMove(int h, int w) {
	if(0 <= h && 0 <= w && h < H && w < W)
		if(m[h][w] == '.' || m[h][w] == 'X')
			return true;
	return false;
}

char decideDirection(int h, int w, char d2) {
	REP(i, 4) {
		int nd = (d.find(d2) + 3 + i) % 4;
		int nh = h + dy[nd], nw = w + dx[nd];
		if(canMove(nh, nw)) return d[nd];
	}
	return d2;
}

int main() {
	while(cin >> W >> H, W|H) {
		ppl = 0;
		REP(i, H) REP(j, W) {
			cin >> m[i][j];
			if(d.find(m[i][j]) != string::npos)
				ppl++;
		}

		int cnt = 0;
		while(ppl) {
			REP(i, H) REP(j, W) {
				if(d.find(m[i][j]) != string::npos) {
					m[i][j] = decideDirection(i, j ,m[i][j]);
				}
			}

			bool chk[30][30];
			memset(chk, true, sizeof(chk));
			REP(i, 4) REP(j, H) REP(k, W) {
				if(m[j][k] == d[i] && chk[j][k]) {
					int nh = j + dy[i], nw = k + dx[i];
					if(m[nh][nw] == '.' && chk[nh][nw]) {
						swap(m[j][k], m[nh][nw]);
						chk[nh][nw] = false;
					}
					else if(m[nh][nw] == 'X' && chk[nh][nw]) {
						m[j][k] = '.';
						chk[nh][nw] = false;
						ppl--;
					}
					chk[j][k] = false;
				}
			}
			cnt++;
			if(cnt > 180) break;
		}
		if(cnt > 180) cout << "NA" << endl;
		else cout << cnt << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
const int L = 4;

int W, H;
int my[] = {0, -1, 0, 1};
int mx[] = {1, 0, -1, 0};
map<char, int> ctoi;

class C{
  public:
    int y, x, d, f, g;
    C(){ f = true; }
    C(int yy, int xx, int dd) { y = yy; x = xx; d = dd; f = true; g = false; }
    P front() { return P(y + my[d], x + mx[d]); }
    void move() { y = y + my[d]; x = x + mx[d]; }
};

bool isPerson(int y, int x, vector<string> &v) { return (v[y][x] != '#' && v[y][x] != '.' && v[y][x] != 'X'); }

bool canMoveBeforeTurn(int tar, vector<string> &v, vector<C> &ps){
  P next = ps[tar].front();
  if(v[next.first][next.second] != '.' && v[next.first][next.second] != 'X') return false;
  REP(i, ps.size()) if(i != tar && ps[i].f && next == P(ps[i].y, ps[i].x)) return false;
  return true;
}

bool check(int tar, int n, vector<C> &ps){
  vector<int> v(4);
  v[0] = 2; v[1] = 3; v[2] = 0; v[3] = 1;
  return (v[tar] < v[n]);
}

bool canMoveAfterTurn(int tar, vector<C> &ps){
  REP(i, ps.size()){
    if(!ps[i].f || tar == i) continue;
    if(ps[i].front() == ps[tar].front() && !check(ps[tar].d, ps[i].d, ps)) return false;
  }
  return true;
}

bool isEnd(vector<C> &ps){
  REP(i, ps.size()) if(ps[i].f) return false;
  return true;
}

void disp(vector<string> &v, vector<C> &ps){
  vector<string> vv = v;
  REP(i, ps.size()){
    if(!ps[i].f) continue;
    if(ps[i].d == 0) vv[ps[i].y][ps[i].x] = 'E';
    if(ps[i].d == 1) vv[ps[i].y][ps[i].x] = 'N';
    if(ps[i].d == 2) vv[ps[i].y][ps[i].x] = 'W';
    if(ps[i].d == 3) vv[ps[i].y][ps[i].x] = 'S';
  }
  cout <<endl;
  REP(i, v.size()) cout <<vv[i] <<endl;
}

int main() {
  ctoi['E'] = 0; ctoi['N'] = 1; ctoi['W'] = 2; ctoi['S'] = 3;
  while(cin >>W >>H && W && H){
    vector<string> v(H);
    REP(i, H) cin >>v[i];
    vector<C> ps;
    REP(i, H) REP(j, W) if(isPerson(i, j, v)) { ps.push_back(C(i, j, ctoi[v[i][j]])); v[i][j] = '.'; }

    int res = 0;
    while(!isEnd(ps) && res++ < 180){
      //disp(v, ps);
      REP(i, ps.size()){
        if(!ps[i].f) continue;
        int nds[] = {(ps[i].d + 3) % L, ps[i].d, (ps[i].d + 1) % L, (ps[i].d + 2) % L};
        for(int nd : nds){
          ps[i].d = nd;
          if(canMoveBeforeTurn(i, v, ps)) break;
        }
      }
      REP(i, ps.size()) if(ps[i].f && canMoveAfterTurn(i, ps)) ps[i].g = 1;
      REP(i, ps.size()){
        if(!ps[i].f || ps[i].g == 0) continue;
        ps[i].g = 0;
        ps[i].move();
        if(v[ps[i].y][ps[i].x] == 'X') ps[i].f = false;
      }
    }
    if(res > 180) cout <<"NA" <<endl;
    else cout <<res <<endl;
  }
  return 0;
}