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 <bits/stdc++.h>

using namespace std;

#define REP(i,a,b) for(int i=a;i<int(b);i++)
#define rep(i,n) REP(i,0,n)

int W, H;
vector<string> G;
vector<tuple<int, int, int>> Ps;// y, x, dir

string const DIRS = "ENWS";
int dx[] = {1,0,-1,0};
int dy[] = {0,-1,0,1};

bool in_range(int y, int x) {
  return 0<=y&&y<H && 0<=x&&x<W;
}

bool adjacent(int i, int j, int y, int x, int dir) {
  const int ny = y + dy[dir], nx = x + dx[dir];
  if(i == ny && j == nx) return true;
  return false;
}

int get_dir(int i, int j, int y, int x) {
  // precondition adjacent = 1;
  rep(k, 4) {
    int ni = i + dy[k], nj = j + dx[k];
    if(ni == y && nj == x) {
      return k;
    }
  }
  assert(false);
}

void move_person(int pid) {
  int y, x, d; tie(y, x, d) = Ps[pid];
  Ps[pid] = make_tuple(y + dy[d], x + dx[d], d);
}

bool is_on_exit(int pid) {
  int y, x, _; tie(y, x, _) = Ps[pid];
  if(G[y][x] == 'X') return true;
  return false;
}

int simulate() {
  
  int ans = 0;
  
  while(!Ps.empty() && ans <= 180) {
#if 0
    rep(i, Ps.size()) {
      cout << "pid: " << i << endl;
      int y, x, d; tie(y, x, d) = Ps[i];
      cout << "(" << y << ", " << x << ", " << d << ")" << endl;
    }
    cout << "------" << endl;
#endif

    set<int> pset;
    vector<pair<int, int>> V;

    vector<tuple<int, int, int>> nps = Ps;
    
    rep(i, Ps.size()) {
      int y, x, dir; tie(y, x, dir) = Ps[i];
      int ndir = -1;
      for(int k=dir+3; k<dir+7; k++) {
	int ny = y + dy[k%4], nx = x + dx[k%4];
	if(!in_range(ny, nx)) continue;
	if(G[ny][nx] == '#') continue;
	
	bool ng = 0;
	rep(pid, Ps.size()) {
	  if(pid == i) continue;
	  int yy, xx, dd; tie(yy, xx, dd) = Ps[pid];
	  int ny = y + dy[k%4], nx = x + dx[k%4];
	  if(yy == ny && xx == nx) ng = 1;
	}
	if(ng) continue;
	
	ndir = k%4;
	break;
      }
      if(ndir != -1) {
	nps[i] = make_tuple(y, x, ndir);
	V.emplace_back(y+dy[ndir], x+dx[ndir]);
      }
    }
    
    Ps = nps;

    set<int> movers;
    
    rep(ii, V.size()) {
      int i = V[ii].first, j = V[ii].second;
      vector<pair<int, int>> vs; 
      rep(pid, Ps.size()) {
	int y, x, dir; tie(y, x, dir) = Ps[pid];
	if(adjacent(i, j, y, x, dir)) {
	  vs.emplace_back(get_dir(i, j, y, x), pid);
	}
      }
      
      if(!vs.empty()) {
	sort(vs.begin(), vs.end());
	int pid = vs[0].second;
	movers.insert(pid);
      }
    }

    for(auto && e: movers) {
      move_person(e);
    }

    vector<tuple<int, int, int>> NPs;
    rep(i, Ps.size()) {
      if(!is_on_exit(i)) {
	NPs.push_back(Ps[i]);
      }
    }
    Ps = NPs;
    ans ++;
  }

  if(ans > 180) return -1;
  
  return ans;
}

int main() {
  
  while(cin >> W >> H && (W | H)) {
    G.clear();
    G.resize(H);
    rep(i, H) {
      cin >> G[i];
    }
    
    Ps.clear();
    
    rep(i, H) rep(j, W) {
      if(DIRS.find(G[i][j]) != string::npos) {
	int dir = DIRS.find(G[i][j]);
	int y = i, x = j;
	Ps.emplace_back(y, x, dir);
      }
    }
    
    int r = simulate();
    if(r == -1) {
      cout << "NA" << endl;
    } else {
      cout << r << endl;
    }
  }
  
  return 0;
}