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>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit
#include <cstdio>	// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define EAST 0
#define NORTH 1
#define WEST 2
#define SOUTH 3
#define DEBUG 0

using namespace std;

typedef pair<int, int> P;	
/*
typedef pair<P, P> PP;ツ　// ツ個サツ催敖つ「ツづゥツ暗環置, ツ篠淞づ個暗環置  Pi (P (crow, ccol ), P (nr, nc ) )
typedef pair<PP, int> PPi; // ツ個サツ催敖づ個暗環置ツ、ツ篠淞づ個暗環置ツ、ツ個サツ催敖古シツつ「ツづつ「ツづゥツづ個陛サツ古シ
*/
struct Person{
	int nrow, ncol;
	int dir;
	bool canmove;
};
const int timeout = 180;// default: 180;
int w, h;
char mize[32][32];

char print_dir (int n )
{
	const char dir[4] = { 'E', 'N', 'W', 'S' };
	return dir[n]; 	
}

void disp_dir (int n )
{
	const string dir[4] = { "EAST", "NORTH", "WEST", "SOUTH" };
	cout << "dir: " << dir[n] << endl;
}

void disp_mize (void )
{
	rep (i, h ){
		rep (j, w ){
			cout << mize[i][j];
		} // end rep
		cout << endl;
	} // end rep
	cout << endl;
}

int main()
{
	const int dir[][2] = { { 0, 1 }, {-1, 0 }, { 0, -1 }, { 1, 0 }  }; // EAST, NORTH, WEST, SOUTH order
//	cut here before submit 
//	freopen ("testcase.Square", "r", stdin );
	map<char, int> Dir;
	Dir['N'] = NORTH, Dir['E'] = EAST, Dir['S'] = SOUTH, Dir['W'] = WEST; 
	while (scanf ("%d %d", &w, &h ) && w && h ){
		memset (mize, 0, sizeof (mize ) );
		set<P> ee;	// ツ氾アツ湘ュツ古サ
		ee.clear();
		rep (i, h ){
			rep (j, w ){
				scanf (" %c", mize[i]+j );
				if (mize[i][j] == 'X' )
					ee.insert (P (i, j ) );
			} // end rep
		} // end rep
		int res = 0;
		bool escape = false;
		while (res <= timeout ){ 
#if DEBUG
			disp_mize();
#endif
			map<P,Person> person;
			person.clear();
//			int id = 0;
			rep (i, h ){
				rep (j, w ) {
					if (isalpha (mize[i][j] ) && mize[i][j] != 'X' ){
						Person p;
						p.nrow = -1, p.ncol = -1, p.dir = Dir[mize[i][j]];
						p.canmove = false;
						person[P(i, j )] = p;
//						mize[i][j] = '.';
					} // end if
				} // end rep
			} // end rep
			if (person.empty() ){
				escape = true;
				break;
			} // end if
			map<P,Person>::iterator it = person.begin();
			for (; it != person.end(); it++ ){
				P pp = (*it).first;
				Person p = (*it).second;
				int crow = pp.first;
				int ccol = pp.second;
				int cdir = p.dir;
				bool ne = false;
				for (int k = (4 + cdir - 1) % 4, cnt = 0;cnt < 4; k = (k + 1 ) % 4, cnt++ ){
					int nr = crow + dir[k][0];
					int nc = ccol + dir[k][1];
					if (nr < 0 || nc < 0 || nr >= h || nc >= w || mize[nr][nc] == '#' ) continue;
					if (isalpha(mize[nr][nc] ) && mize[nr][nc] != 'X' ) continue;
					Person nd;
					nd.nrow = nr, nd.ncol = nc, nd.dir = k;
					nd.canmove = true;
					person[P(crow, ccol)] = nd;
					ne = true;
					break;
				} // end for
				if (!ne ){
					Person nd;
					nd.nrow = crow, nd.ncol = ccol, nd.dir = cdir;
					nd.canmove = false;
					person[P(crow, ccol) ] = nd;
				} // end if
			} // end rep
			set<P> next;
			next.clear();
			for (it = person.begin(); it != person.end(); it++ ){
				Person cp = (*it).second;
				next.insert (P (cp.nrow, cp.ncol ) );
			} // end rep
			if (!next.empty() ){
				set<P>::iterator jt = next.begin();
				for (; jt != next.end(); jt++ ){
					int crow = (*jt).first;
					int ccol = (*jt).second;
					bool can = true;
					rep (k, 4 ){
						int nr = crow + dir[k][0];
						int nc = ccol + dir[k][1];
						if (nr < 0 || nc < 0 || nr >= h || nc >= w || mize[nr][nc] == '#' || mize[nr][nc] == '.' || mize[nr][nc] == 'X' ) continue;
						if (can ){
							person[P (nr, nc )].canmove = true;
							can = false;
						}else{
							person[P (nr, nc )].canmove = false;
						} // end if
					} // end rep
				} // end rep
			} // end if
			for (it = person.begin(); it != person.end(); it++ ){
				P pp = (*it).first;
				Person p = (*it).second;
				if (p.canmove ){
					if (ee.count (P (p.nrow, p.ncol ) ) ){
						mize[pp.first][pp.second] = '.';
						continue;
					} // end if
					mize[pp.first][pp.second] = '.';
					mize[p.nrow][p.ncol] = print_dir (p.dir );				
//				}else{
//					mize[pp.first][pp.second] = '.';
//					mize[pp.first][pp.second] = print_dir (p.dir );
				} // end if
#if DEBUG
				cout << '(' << pp.first << ',' << pp.second << ')' << endl;
				disp_dir (p.dir );
				cout << "next_pos: (" << p.nrow << ',' << p.ncol << ')' << endl;
				cout << "can_move: " << (p.canmove ? "YES" : "NO" ) << endl;
#endif

			} // end for
			res++;
		} // end while
		if (escape ){
			printf ("%d\n", res );
		}else{
			printf ("NA\n" );
		} // end if
	} // end loop

//	printf ("%d\n", res );
		
	return 0;
}