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 <bits/stdc++.h>
using namespace std;

int W,H;
char str[32][32];
bool is_exist;

void print_map(){
	for(int i = 1; i <= H; i++){
		for(int j = 1; j <= W; j++){
			printf("%c",str[i][j]);
		}
		puts("");
	}
}

bool is_movable(int x,int y){
	return str[x][y] == '.' || str[x][y] == 'X';
}

bool change_dir(int x,int y){
	switch(str[x][y]){
		case 'E' :
			if(is_movable(x+1,y)){
				str[x][y] = 'S';
			}
			else if (is_movable(x,y+1)){
				str[x][y] = 'E';
			}
			else if (is_movable(x-1,y)){
				str[x][y] = 'N';
			}
			else if (is_movable(x,y-1)){
				str[x][y] = 'W';
			}
			is_exist = true;
			break;
		case 'N' :
			if(is_movable(x,y+1)){
				str[x][y] = 'E';
			}
			else if(is_movable(x-1,y)){
				str[x][y] = 'N';
			}
			else if (is_movable(x,y-1)){
				str[x][y] = 'W';
			}
			else if (is_movable(x+1,y)){
				str[x][y] = 'S';
			}
			is_exist = true;
			break;
		case 'W' :
			if(is_movable(x-1,y)){
				str[x][y] = 'N';
			}
			else if(is_movable(x,y-1)){
				str[x][y] = 'W';
			}
			else if (is_movable(x+1,y)){
				str[x][y] = 'S';
			}
			else if (is_movable(x,y+1)){
				str[x][y] = 'E';
			}
			is_exist = true;
			break;
		case 'S' :
			if(is_movable(x,y-1)){
				str[x][y] = 'W';
			}
			else if (is_movable(x+1,y)){
				str[x][y] = 'S';
			}
			else if(is_movable(x,y+1)){
				str[x][y] = 'E';
			}
			else if (is_movable(x-1,y)){
				str[x][y] = 'N';
			}
			is_exist = true;
			break;
		default :
			break;
	}
}

void move(int x,int y){
	if(str[x][y] == '.'){
		if(str[x][y+1] == 'W'){
			str[x][y+1] = '*';
			str[x][y] = 'w';
		}
		else if (str[x-1][y] == 'S'){
			str[x-1][y] = '*';
			str[x][y] = 's';
		}
		else if(str[x][y-1] == 'E'){
			str[x][y-1] = '*';
			str[x][y] = 'e';
		}
		else if(str[x+1][y] == 'N'){
			str[x+1][y] = '*';
			str[x][y] = 'n';
		}
	}
	else if(str[x][y] == 'X'){
		if(str[x][y+1] == 'W'){
			str[x][y+1] = '*';
		}
		else if (str[x-1][y] == 'S'){
			str[x-1][y] = '*';
		}
		else if(str[x][y-1] == 'E'){
			str[x][y-1] = '*';
		}
		else if(str[x+1][y] == 'N'){
			str[x+1][y] = '*';
		}
	}
}


void update(){
	for(int i = 1; i <= H; i++){
		for(int j = 1; j <= W; j++){
			if(str[i][j] == '*'){
				str[i][j] = '.';
			}
			else if(str[i][j] == 'w'){
				str[i][j] = 'W';
			}
			else if(str[i][j] == 'n'){
				str[i][j] = 'N';
			}
			else if (str[i][j] == 'e'){
				str[i][j] = 'E';
			}
			else if(str[i][j] == 's'){
				str[i][j] = 'S';
			}
		}
	}
}

int main(){
	while(scanf("%d%d",&W,&H),W != 0 && H != 0){
		bool flg = true;
		for(int i = 0; i < 32; i++){
			memset(str[i],0,32);
		}
		for(int i = 1; i <= H; i++){
			scanf("%s",str[i]+1);
		}
		for(int t = 0; t <= 180; t++){
			//1
			is_exist = false;
			for(int i = 1; i <= H; i++){
				for(int j = 1; j <= W; j++){
					change_dir(i,j);
				}
			}
			if(!is_exist){
				printf("%d\n",t);
				flg = false;
				break;
			}
			//2
			for(int i = 1; i <= H; i++){
				for(int j = 1; j <= W; j++){
					move(i,j);
				}
			}
			update();
			//print_map();
		}
		if(flg){
			printf("NA\n");
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

const int dy[4] = {1, 0, -1, 0};
const int dx[4] = {0, 1, 0, -1};
const string ENWS = "ENWS";

int main() {
    int W, H;
    while (cin >> W >> H, W) {
        vector<string> S(H);
        for (int i = 0; i < H; ++i) cin >> S[i];

        int ans = -1;
        for (int t = 0; t <= 180; ++t) {
            vector<pair<int, int>> pos;
            for (int i = 0; i < H; ++i) {
                for (int j = 0; j < W; ++j) {
                    if (ENWS.find(S[i][j]) != string::npos) {
                        pos.emplace_back(i, j);
                    }
                }
            }
            if (pos.empty()) {
                ans = t;
                break;
            }

            map<pair<int, int>, vector<pair<int, int>>> mp;
            for (int i = 0; i < pos.size(); ++i) {
                int y, x;
                tie(y, x) = pos[i];
                int dir = ENWS.find(S[y][x]);
                for (int j = 0; j < 4; ++j) {
                    int ny = y + dy[(dir + j) % 4];
                    int nx = x + dx[(dir + j) % 4];
                    if (ny >= 0 && ny < H && nx >= 0 && nx < W && (S[ny][nx] == '.' || S[ny][nx] == 'X')) {
                        mp[make_pair(ny, nx)].emplace_back((dir + j + 1) % 4, i);
                        S[y][x] = ENWS[(dir + j + 3) % 4];
                        break;
                    }
                }
            }
            for (auto &p : mp) {
                sort(p.second.begin(), p.second.end());
                int sy, sx, ty, tx;
                tie(sy, sx) = pos[p.second[0].second];
                tie(ty, tx) = p.first;
                if (S[ty][tx] == '.') {
                    S[ty][tx] = S[sy][sx];
                }
                S[sy][sx] = '.';
            }
        }
        if (ans < 0) cout << "NA" << endl;
        else cout << ans << endl;
    }
}