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++: #define _USE_MATH_DEFINES
#define INF 10000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

static const double eps = 1e-8;

char stage[50][50];

static const int moveTo[][2] = {{1,0},{0,-1},{-1,0},{0,1}};
//static const char dir[] = {'N','W','S','E'};


void changeDir(int W,int H){
	map<char,int> dir;
	dir['N'] = 0; dir['W'] = 1; dir['S'] = 2; dir['E'] = 3;
	const char num2dir[] = {'E','N','W','S'};
	//# Ç
	//.(sIh)°
	//X ñíû
	//E ðü¢Ä¢él
	//N kðü¢Ä¢él
	//W ¼ðü¢Ä¢él
	//S ìðü¢Ä¢él

	for(int y=0;y<H;y++){
		for(int x=0;x<W;x++){
			switch(stage[y][x]){
			case '#':			  
				break;
			case 'X':
				break;
			case 'N': case 'W': case 'S': case 'E':
				for(int i=0;i<4;i++){
					int dx = x + moveTo[(i+dir[stage[y][x]])%4][0];
					int dy = y + moveTo[(i+dir[stage[y][x]])%4][1];
					if(dy < 0 || dy >= H || dx < 0 || dx >= W) continue;
					if(stage[dy][dx] == '.' || stage[dy][dx] == 'X'){
						stage[y][x] = num2dir[(i+dir[stage[y][x]])%4];
						break;
					}
				}

				break;
			default :
				break;
			}

		}
	}
}

void movePlayer(int W,int H){
	map<char,int> dir;
	dir['N'] = 0; dir['W'] = 1; dir['S'] = 2; dir['E'] = 3;
	const char num2other_dir[] = {'W','S','E','N'};
	//# Ç
	//.(sIh)°
	//X ñíû
	//E ðü¢Ä¢él
	//N kðü¢Ä¢él
	//W ¼ðü¢Ä¢él
	//S ìðü¢Ä¢él

	bool used[30][30];
	fill((bool*)used,(bool*)used+30*30,false);
	for(int dy=0;dy<H;dy++){
		for(int dx=0;dx<W;dx++){
			
			switch(stage[dy][dx]){
			case '#':			  
				break;
			case 'X':
				for(int i=0;i<4;i++){
					int sx = dx + moveTo[i][0];
					int sy = dy + moveTo[i][1];
					
					if(sy < 0 || sy >= H || sx < 0 || sx >= W) continue;
					if(used[sy][sx]) continue;
					if(stage[sy][sx] == num2other_dir[i]){
						stage[sy][sx] = '.';
						used[dy][dx] = true;
						used[sy][sx] = true;
						break;
					}
				}
				break;
			case '.':
				for(int i=0;i<4;i++){
					int sx = dx + moveTo[i][0];
					int sy = dy + moveTo[i][1];
					
					if(sy < 0 || sy >= H || sx < 0 || sx >= W) continue;
					if(used[sy][sx]) continue;
					if(stage[sy][sx] == num2other_dir[i]){
						stage[dy][dx] = num2other_dir[i];
						stage[sy][sx] = '.'; 
						used[dy][dx] = true;
						used[sy][sx] = true;
						break;
					}
				}
				break;
			case 'N': case 'W': case 'S': case 'E':
				break;
			default :
				break;
			}

		}
	}
}

void printStage(int W,int H){
	for(int y=0;y<H;y++){
		for(int x=0;x<W;x++){
			printf("%c",stage[y][x]);
		}
		printf("\n");
	}
}

bool isClear(int W,int H){
	for(int y=0;y<H;y++){
		for(int x=0;x<W;x++){
			switch(stage[y][x]){
			case 'N': case 'W': case 'S': case 'E':
				return false;
				break;
			default:
				break;
			}
		}
	}
	return true;
}

int main(){
	int W,H;
	while(~scanf("%d %d",&W,&H)){
		if(W==H && H==0) break;
		memset(stage,'#',sizeof(stage));
		for(int y=0;y<H;y++){
			char buf[64];
			scanf("%s",buf);
			for(int x=0;x<W;x++){
				stage[y][x] = buf[x];
			}
		}

		int res=0;
		//printStage(W,H);
		while(!isClear(W,H)){
			
 			changeDir(W,H);
			//printStage(W,H);
 			movePlayer(W,H);
			//printStage(W,H);
			res++;
			if(res > 180) break;
		}

		if(res > 180) printf("NA\n");
		else printf("%d\n",res);

	}

}

C++: #include<stdio.h>
#include<string.h>
#include<vector>
int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
struct S{int x,y,a;S(){}S(int x,int y,int a):x(x),y(y),a(a){}};
int F(char c){return c=='.'||c=='X';}
int main()
{
	char*p,a[32][32];
	int W,H,x,y,t,i,j;
	while(scanf("%d%d",&W,&H),W)
	{
		std::vector<S>v,u[4];
		for(y=0;y<H;++y)
		{
			scanf("%s",a[y]);
			for(p=a[y];p=strpbrk(p,"NEWS");++p)
				v.push_back(S(p-a[y],y,*p=='W'?0:*p=='S'?1:*p=='E'?2:3));
		}
		for(t=0;t<181&&v.size();++t)
		{
			for(i=0;i<4;++i)u[i].clear();
			for(i=0;i<v.size();++i)
			{
				S s=v[i];
				for(j=0;j<4;++j)
				{
					x=s.x+dx[(s.a+3+j)&3];
					y=s.y+dy[(s.a+3+j)&3];
					if(F(a[y][x]))
					{
						s.a=(s.a+3+j)&3;
						u[s.a].push_back(s);
						goto N;
					}
				}
				v.push_back(s);
				N:;
			}
			v.clear();
			for(i=0;i<4;++i)
			{
				for(j=0;j<u[i].size();++j)
				{
					S s=u[i][j];
					x=s.x+dx[i];
					y=s.y+dy[i];
					if(F(a[y][x]))
					{
						a[s.y][s.x]='.';
						if(a[y][x]=='X')
							continue;
						a[y][x]='@';
						s.x=x;
						s.y=y;
					}
					v.push_back(s);
				}
			}
		}
		if(t<181)printf("%d\n",t);
		else puts("NA");
	}
	return 0;
}