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 <algorithm>
#include <string>

using namespace std;

int main(){


	char ag[200];
	ag['W']=0;
	ag['S']=1;
	ag['E']=2;
	ag['N']=3;
	ag[0]='W';
	ag[1]='S';
	ag[2]='E';
	ag[3]='N';

	char field[31][31];
	int h,w;
	while(cin>>w>>h&&!(w==0&&h==0)){
		int left=0;
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin>>field[i][j];
				if(field[i][j]=='W'||field[i][j]=='S'||field[i][j]=='N'||field[i][j]=='E'){
					left++;
				}
			}
		}
		bool na=true;
		for(int i = 0; i < 180; i++){
			//for(int i = 0; i < h; i++){
			//	for(int j =0 ; j < w; j++){
			//		cout<<field[i][j];
			//	}
			//	cout<<endl;
			//}

			vector<char> moves[31][31];
			for(int j = 0; j < h; j++){
				for(int k = 0; k < w; k++){
					if(field[j][k]=='W'||field[j][k]=='S'||field[j][k]=='N'||field[j][k]=='E'){
						pair<int,int> p;
						if(field[j][k]=='W'){
							p.first=-1;
							p.second=0;
						}
						else if(field[j][k]=='S'){
							p.first=0;
							p.second=-1;
						}
						else if(field[j][k]=='N'){
							p.first=0;
							p.second=1;
						}
						else if(field[j][k]=='E'){
							p.first=1;
							p.second=0;
						}

						bool fin=false;
						for(int l = 0; l < 4; l++){
							if(field[j+p.first][k+p.second]=='.'||field[j+p.first][k+p.second]=='X'){
								fin=true;
								//cout<<field[j][k]<<endl;
								field[j][k]=(char)((ag[(ag[field[j][k]]+l+3)%4]));
								moves[j+p.first][k+p.second].push_back(field[j][k]);
								//cout<<(char)((ag[(ag[field[j][k]]+l+3)%4]))<<endl;
								break;
							}
							// ñ]
							int tmp=p.second;
							p.second=p.first;
							p.first=-tmp;
						}

						// àµÚ®æª©Â©ÁÄ¢½ç
						if(fin){
							
						}
					}
				}
			}
			// Ú®óâÌXgð©ÄADæxÌ¢àÌðÚ®AÜ½ÍS[³¹é
			for(int j = 0; j < h; j++){
				for(int k = 0; k < w; k++){
					if(k==2&&j==1){
					//	cout<<endl;
					}
					bool flags[200];
					fill(flags,flags+200,false);
					for(int l = 0; l < moves[j][k].size(); l++){
						flags[moves[j][k][l]]=true;
					}
					// W,S,E,NÌÔÉDæxª é
					if(flags['W']){
						field[j][k+1]='.';
						if(field[j][k]=='X'){
							left--;
						}
						else
							field[j][k]='W';
					}
					else if(flags['S']){
						field[j-1][k]='.';
						if(field[j][k]=='X'){
							left--;
						}
						else
							field[j][k]='S';
					}
					else if(flags['E']){
						field[j][k-1]='.';
						if(field[j][k]=='X'){
							left--;
						}
						else
							field[j][k]='E';
					}
					else if(flags['N']){
						field[j+1][k]='.';
						if(field[j][k]=='X'){
							left--;
						}
						else
							field[j][k]='N';
					}
				}
			}
			if(left==0){
				na=false;
				cout<<i+1<<endl;
				break;
			}
		}
		if(na)
			cout<<"NA"<<endl;
	}

	return 0;
}