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>
using namespace std;

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

int w,h;
int t[31][31],s[31][31];
int di[] = {-1,0,1,0};
int dj[] = {0,1,0,-1};

void chDir(void){
	rep(i,h)rep(j,w)if(t[i][j]>=0)for(int k=1;k>=-2;k--){
		int nd = (t[i][j] + 4 + k) % 4;
		int ni = i + di[nd];
		int nj = j + dj[nd];

		if(t[ni][nj] == -1 || t[ni][nj] == -2){
			t[i][j] = nd;
			break;
		}
	}
}

void printMapT(void){
	rep(i,h){
		rep(j,w){
			if(t[i][j] == 0) cout<<"N";
			if(t[i][j] == 1) cout<<"E";
			if(t[i][j] == 2) cout<<"S";
			if(t[i][j] == 3) cout<<"W";
			if(t[i][j] == -1) cout<<"X";
			if(t[i][j] == -2) cout<<".";
			if(t[i][j] == -3) cout<<"#";
		}
		cout<<endl;
	}
	cout<<endl;
}

void printMapS(void){
	rep(i,h){
		rep(j,w){
			if(s[i][j] == 0) cout<<"N";
			if(s[i][j] == 1) cout<<"E";
			if(s[i][j] == 2) cout<<"S";
			if(s[i][j] == 3) cout<<"W";
			if(s[i][j] == -1) cout<<"X";
			if(s[i][j] == -2) cout<<".";
			if(s[i][j] == -3) cout<<"#";
		}
		cout<<endl;
	}
	cout<<endl;
}

int move(void){
	int res = 0;

	rep(i,h)rep(j,w)s[i][j] = t[i][j];

	rep(i,h)rep(j,w)if(t[i][j] == -1 || t[i][j] == -2)for(int k=1;k>=-2;k--){
		int nd = (k + 4) % 4;
		int ni = i + di[nd];
		int nj = j + dj[nd];

		if(ni>=0 && ni<h && nj>=0 && nj<w && t[ni][nj]>=0 && t[ni][nj]==(nd+2)%4){
			if(t[i][j] == -1) res++;
			else s[i][j] = t[ni][nj];
			s[ni][nj] = -2;
			break;
		}
	}

	rep(i,h)rep(j,w)t[i][j] = s[i][j];

	return res;
}

int main(void){
	while(cin>>w>>h && (w || h)){
		int rem = 0;
		char ch;

		rep(i,h){
			rep(j,w){
				cin>>ch;
				if(ch == 'X') t[i][j] = -1;
				else if(ch == '.') t[i][j] = -2;
				else if(ch == '#') t[i][j] = -3;
				else if(ch == 'N'){t[i][j] = 0; rem++;}
				else if(ch == 'E'){t[i][j] = 1; rem++;}
				else if(ch == 'S'){t[i][j] = 2; rem++;}
				else if(ch == 'W'){t[i][j] = 3; rem++;}
			}
		}

		int ans;
		for(ans=0;ans<180 && rem!=0;ans++){
			chDir();
			rem -= move();
		}
		if(ans == 180) cout<<"NA\n";
		else cout<<ans<<endl;
	}

	return 0;
}