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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int diff_row[4] = {0,-1,0,1},diff_col[4] = {1,0,-1,0};
int diff_row_N[4] = {0,-1,0,1},diff_col_N[4] = {1,0,-1,0};
int diff_row_E[4] = {1,0,-1,0},diff_col_E[4] = {0,1,0,-1};
int diff_row_S[4] = {0,1,0,-1},diff_col_S[4] = {-1,0,1,0};
int diff_row_W[4] = {-1,0,1,0},diff_col_W[4] = {0,-1,0,1};

char array[4] = {'W','S','E','N'};

int W,H;
char map[30][31],calc[128];


bool rangeCheck(int row,int col){
	if(row >= 0 && row <= H-1 && col >= 0 && col <= W-1)return true;
	else{
		return false;
	}
}

void func(){

	int num = 0;

	for(int i = 0; i < H; i++){
		scanf("%s",map[i]);
		for(int k = 0; k < W; k++){
			if(map[i][k] == 'N' || map[i][k] == 'E' || map[i][k] == 'S' || map[i][k] == 'W')num++;
		}
	}

	int ans = -1;
	int next_row,next_col;

	for(int time = 1; time <= 180; time++){
		for(int row = 0; row < H; row++){
			for(int col = 0; col < W; col++){
				switch(map[row][col]){
				case 'N':
					for(int i = 0; i < 4; i++){
						next_row = row + diff_row_N[i];
						next_col = col + diff_col_N[i];
						if(map[next_row][next_col] == '.' || map[next_row][next_col] == 'X'){
							switch(i){
							case 0:
								map[row][col] = 'E';
								break;
							case 1:
								//Do nothing
								break;
							case 2:
								map[row][col] = 'W';
								break;
							case 3:
								map[row][col] = 'S';
								break;
							}
							break;
						}
					}
					break;
				case 'E':
					for(int i = 0; i < 4; i++){
						next_row = row + diff_row_E[i];
						next_col = col + diff_col_E[i];
						if(map[next_row][next_col] == '.' || map[next_row][next_col] == 'X'){
							switch(i){
							case 0:
								map[row][col] = 'S';
								break;
							case 1:
								//Do nothing
								break;
							case 2:
								map[row][col] = 'N';
								break;
							case 3:
								map[row][col] = 'W';
								break;
							}
							break;
						}
					}
					break;
				case 'S':
					for(int i = 0; i < 4; i++){
						next_row = row + diff_row_S[i];
						next_col = col + diff_col_S[i];
						if(map[next_row][next_col] == '.' || map[next_row][next_col] == 'X'){
							switch(i){
							case 0:
								map[row][col] = 'W';
								break;
							case 1:
								//Do nothing
								break;
							case 2:
								map[row][col] = 'E';
								break;
							case 3:
								map[row][col] = 'N';
								break;
							}
							break;
						}
					}
					break;
				case 'W':
					for(int i = 0; i < 4; i++){
						next_row = row + diff_row_W[i];
						next_col = col + diff_col_W[i];
						if(map[next_row][next_col] == '.' || map[next_row][next_col] == 'X'){
							switch(i){
							case 0:
								map[row][col] = 'N';
								break;
							case 1:
								//Do nothing
								break;
							case 2:
								map[row][col] = 'S';
								break;
							case 3:
								map[row][col] = 'E';
								break;
							}
							break;
						}
					}
					break;
				}
			}
		}

		for(int row = 0; row < H; row++){
			for(int col = 0; col < W; col++){
				if(map[row][col] == '.'){
					for(int i = 0; i < 4; i++){
						next_row = row + diff_row[i];
						next_col = col + diff_col[i];

						if(rangeCheck(next_row,next_col) == true && map[next_row][next_col] == array[i]){
							map[row][col] = calc[map[next_row][next_col]];
							map[next_row][next_col] = '@';
							break;
						}
					}
				}else if(map[row][col] == 'X'){
					for(int i = 0; i < 4; i++){
						next_row = row + diff_row[i];
						next_col = col + diff_col[i];

						if(rangeCheck(next_row,next_col) == true && map[next_row][next_col] == array[i]){
							map[next_row][next_col] = '@';
							num--;
							break;
						}
					}
				}
			}
		}

		for(int row = 0; row < H; row++){
			for(int col = 0; col < W; col++){
				switch(map[row][col]){
				case 'n':
				case 'e':
				case 's':
				case 'w':
					map[row][col] = calc[map[row][col]];
					break;
				case '@':
					map[row][col] = '.';
					break;
				}
			}
		}

		if(num == 0){
			ans = time;
			break;
		}
	}

	if(ans == -1){
		printf("NA\n");
	}else{
		printf("%d\n",ans);
	}
}

int main(){

	calc['N'] = 'n';
	calc['E'] = 'e';
	calc['S'] = 's';
	calc['W'] = 'w';
	calc['n'] = 'N';
	calc['e'] = 'E';
	calc['s'] = 'S';
	calc['w'] = 'W';

	while(true){
		scanf("%d %d",&W,&H);
		if(H == 0 && W == 0)break;

		func();
	}

	return 0;
}