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 <cstring>
#include <cassert>
#include <vector>
#include <string>

using namespace std;

#define MAX_H 30
#define MAX_W 30

int W, H;
string F[MAX_H];
bool flag[MAX_H][MAX_W];
int t;

bool person(int y, int x) {
    return (F[y][x] == 'E' || F[y][x] == 'N' || F[y][x] == 'W' || F[y][x] == 'S');
}

bool finish() {
    for (int i = 0; i < H; i++) 
        for (int j = 0; j < W; j++)
            if (person(i, j))
                return false;
    return true;
}

char next_dir(char dir) {
    switch (dir) {
        case 'E':
            return 'N';
        case 'N':
            return 'W';
        case 'W':
            return 'S';
        case 'S':
            return 'E';
    }
}

char prev_dir(char dir) {
    switch (dir) {
        case 'E':
            return 'S';
        case 'N':
            return 'E';
        case 'W':
            return 'N';
        case 'S':
            return 'W';
    }
}

bool forward_free(int y, int x, char dir) {
    switch (dir) {
        case 'E':
            if (0 <= x + 1 && x + 1 < W && (F[y][x+1] == '.' || F[y][x+1] == 'X')) return true;
            return false;
        case 'N':
            if (0 <= y - 1 && y - 1 < H && (F[y-1][x] == '.' || F[y-1][x] == 'X')) return true;
            return false;
        case 'W':
            if (0 <= x - 1 && x - 1 < W && (F[y][x-1] == '.' || F[y][x-1] == 'X')) return true;
            return false;
        case 'S':
            if (0 <= y + 1 && y + 1 < H && (F[y+1][x] == '.' || F[y+1][x] == 'X')) return true;
            return false;
    }
}

bool mark_forward(int y, int x, char dir) {
    switch (dir) {
        case 'E':
            flag[y][x+1] = true;
            break;
        case 'N':
            flag[y-1][x] = true;
            break;
        case 'W':
            flag[y][x-1] = true;
            break;
        case 'S':
            flag[y+1][x] = true;
            break;
    }
}

void step() {
    memset(flag, 0, sizeof(flag));
    if (false) {
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                cout << F[i][j] << ' ';
            }
            cout << endl;
        }
    }
    t++;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (person(i, j)) {
                char dir = prev_dir(F[i][j]);
                for (int k = 0; k < 4; k++) {
                    if (forward_free(i, j, dir)) {
                        F[i][j] = dir;
                        mark_forward(i, j, dir);
                        break;
                    }
                    dir = next_dir(dir);
                }
            }
        }
    }

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (flag[i][j]) {
                if (person(i, j+1)) {
                    if (F[i][j] != 'X') F[i][j] = F[i][j+1];
                    F[i][j+1] = '.';
                } else if (person(i-1, j)) {
                    if (F[i][j] != 'X') F[i][j] = F[i-1][j];
                    F[i-1][j] = '.';
                } else if (person(i, j-1)) {
                    if (F[i][j] != 'X') F[i][j] = F[i][j-1];
                    F[i][j-1] = '.';
                } else if (person(i+1, j)) {
                    if (F[i][j] != 'X') F[i][j] = F[i+1][j];
                    F[i+1][j] = '.';
                } else {
                    assert(false);
                }
            }
        }
    }
}

int main() {
    while (cin >> W >> H, W || H) {
        t = 0;
        for (int i = 0; i < H; i++) cin >> F[i];
        while (!finish() && t <= 180) {
            step();
        }
        if (t == 181) cout << "NA" << endl;
        else cout << t << endl;
    }
    return 0;
}