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>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;

int w, h;
char s[30][31], b[30][31];
map<char, int>d{ {'N',0},{'E',1},{'S',2},{'W',3} };
char rd[] = "NESW";
int dx[]{ -1,0,1,0 }, dy[]{ 0,1,0,-1 }, a[]{ 1,0,3,2 };
int main() {
	while (scanf("%d%d", &w, &h), h) {
		rep(i, h)scanf("%s", s[i]);
		for (int i = 0; i <= 180; i++) {
			bool ok = true;
			rep(i, h)rep(j, w)for (auto p : d) {
				if (s[i][j] == p.first) { ok = false; break; }
			}
			if (ok) { printf("%d\n", i); goto g; }
			rep(i, h)rep(j, w) {
				if (d.find(s[i][j]) == d.end())continue;
				int b = d[s[i][j]];
				rep(k, 4) {
					int nx = i + dx[(b + a[k]) % 4], ny = j + dy[(b + a[k]) % 4];
					if (0 <= nx&&nx < h && 0 <= ny&&ny < w && (s[nx][ny] == '.' || s[nx][ny] == 'X')) {
						s[i][j] = rd[(b + a[k]) % 4]; break;
					}
				}
			}
			rep(i, h)rep(j, w)b[i][j] = s[i][j];
			rep(i, h)rep(j, w) {
				if (s[i][j] != 'X'&&s[i][j] != '.')continue;
				rep(k, 4) {
					int nx = i + dx[a[k]], ny = j + dy[a[k]];
					if (0 <= nx&&nx < h && 0 <= ny&&ny < w&&d.find(s[nx][ny]) != d.end() && nx + dx[d[s[nx][ny]]] == i&&ny + dy[d[s[nx][ny]]] == j) {
						if (s[i][j] == 'X')b[nx][ny] = '.';
						else swap(b[i][j], b[nx][ny]);
						break;
					}
				}
			}
			rep(i, h)rep(j, w)s[i][j] = b[i][j];
		}
		puts("NA");
	g:;
	}
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
// E, N, W, S
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
string ENWS = "ENWS";

int main(){
  int W, H;
  while(cin >> W >> H && W){
    vector<string> grid(H);
    REP(y, H) cin >> grid[y];
    REP(y, H) REP(x, W) if(grid[y][x] == 'X') grid[y][x] = '%';
    int rest = 0;
    REP(y, H) REP(x, W) if(isupper(grid[y][x])) rest++;
    int ans = INF;
    REP(time, 181){
      if(rest == 0) {
        ans = time;
        break;
      }
      REP(y, H) REP(x, W){
        REP(i, 4) if(grid[y][x] == ENWS[i]){
          REP(r, 4){
            int rr = (i + r + 3) % 4;
            int nx = x + dx[rr];
            int ny = y + dy[rr];
            if(valid(nx, ny, W, H) && (grid[ny][nx] == '.' || grid[ny][nx] == '%')){
              grid[y][x] = ENWS[rr];
              goto LOOPOUT;
            }
          }
        }
        LOOPOUT:;
      }
      vector<string> next = grid;
      REP(y, H) REP(x, W) if(grid[y][x] == '.' || grid[y][x] == '%') REP(r, 4){
        int nx = x + dx[r];
        int ny = y + dy[r];
        if(valid(nx, ny, W, H) && grid[ny][nx] == ENWS[(r + 2) % 4]){
          next[ny][nx] = '.';
          if(grid[y][x] == '.') next[y][x] = grid[ny][nx];
          else rest--;

          break;
        }
      }
      grid = next;
    }
    if(ans == INF) cout << "NA" << endl;
    else cout << ans << endl;
  }
  return 0;
}

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;

			//debug
//			REP(i, H) {
//				REP(j, W)
//					cout << m[i][j];
//				cout << endl;
//			}
		}
		if(cnt > 180) cout << "NA" << endl;
		else cout << cnt << endl;
	}
	return 0;
}