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 <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef vector<int> VI;
typedef vector<VI> VVI;
template<class T> T gcd( T a, T b ) {
	return !b ? a : gcd( b, a % b );
}
template<class T> T lcm( T a, T b ) {
	return a / gcd( a, b ) * b;
}
template<class T> string print_v( vector<T> v ) {
	OSS oss;
	for ( typename vector<T>::iterator it_i = v.begin(); it_i != v.end();
			++it_i ) {
		oss << *it_i << ", ";
	}
	return oss.str();
}

const int dr[4] = { 0, -1, 0, 1 };
const int dc[4] = { 1, 0, -1, 0 };
const string dt = "ENWS"; // 東、北、西、南
map <char, int> ID;

int w, h;

bool is_empty_cell( const VS& M, const int r, const int c ) {
    return M[r][c] == '.' || M[r][c] == 'X';
}

bool is_obj_cell( const VS& M, const int r, const int c ) {
    return M[r][c] == '#';
}

bool is_human_cell( const VS& M, const int r, const int c ) {
    return ! is_empty_cell( M, r, c ) && M[r][c] != '#';
}

bool is_exit_cell( const VS& M, const int r, const int c ) {
    return M[r][c] == 'X';
}

void debug_print( const VS& M ) {
    static int cnt = 0;
    cout << ( ++ cnt ) << ": " << endl;
    for ( int i = 0; i < h; ++ i ) {
        for ( int j = 0; j < w; ++ j ) {
            cout << M[i][j];
        }
        cout << endl;
    }
    cout << endl;
}

VS move( VS M ) {
    // debug_print( M );
    int cnt = 0;
    for ( int i = 0; i < h; ++ i ) {
        for ( int j = 0; j < 4; ++ j ) {
            cnt += count( M[i].begin(), M[i].end(), dt[j] );
        }
    }
    // その1
    for ( int i = 0; i < h; ++ i ) {
        for ( int j = 0; j < w; ++ j ) {
            if ( ! is_human_cell( M, i, j ) ) continue;
            ([]( VS& res, int r, int c ) {
                char now = res[r][c];
                // cout << r << ", " << c << ", " << now << ", " << ID[now] << endl;
                for ( int i = 0; i < 4; ++ i ) {
                    int k = ( ID[now] + i ) % 4;
                    int nr = r + dr[k];
                    int nc = c + dc[k];
                    if ( nr < 0 || nr >= h ) continue;
                    if ( nc < 0 || nc >= w ) continue;
                    if ( is_empty_cell( res, nr, nc ) ) {
                        res[r][c] = dt[k];
                        // cout << "from: " << r << ", " << c << ", " << now << ", " << ( ID[now] + i ) % 4;
                        // cout << " => " << nr << ", " << nc << ", " << dt[k] << ", " << k << endl;
                        return;
                    }
                }
            })( M, i, j );
        }
    }
    // その2
    VS res = M;
    for ( int i = 0; i < h; ++ i ) {
        for ( int j = 0; j < w; ++ j ) {
            if ( ! is_empty_cell( res, i, j ) ) continue;
            ([]( VS& M, VS& res, int r, int c ) {
                for ( int i = 0; i < 4; ++ i ) {
                    int nr = r + dr[i];
                    int nc = c + dc[i];
                    if ( nr < 0 || nr >= h ) continue;
                    if ( nc < 0 || nc >= w ) continue;
                    int adj = ( i + 2 ) % 4;
                    if ( M[nr][nc] == dt[adj] ) {
                        if ( ! is_exit_cell( M, r, c ) )
                            res[r][c] = M[nr][nc];
                        res[nr][nc] = '.';
                        M[nr][nc] = '.';
                        return;
                    }
                }
            })( M, res, i, j );
        }
    }
    // debug_print( res );
    // cout << cnt << endl;
    return res;
}

bool is_empty_field( VS& M ) {
    for ( int i = 0; i < h; ++ i ) {
        for ( int j = 0; j < w; ++ j ) {
            if ( is_human_cell( M, i, j ) ) return false;
        }
    }
    return true;
}

int solve( VS M ) {
    if ( is_empty_field( M ) )
        return 0;
    for ( int i = 1; i <= 180; ++ i ) {
        M = move( M );
        if ( is_empty_field( M ) )
            return i;
    }
    return -1;
}

void init_ID() {
    ID.clear();
    ID['E'] = 3;
    ID['N'] = 0;
    ID['W'] = 1;
    ID['S'] = 2;
}

int main() {
    init_ID();
    
    while ( cin >> w >> h && w ) {
        // cout << "test: " << w << ", " << h << endl;
        VS lines(h);
        for ( int i = 0; i < h; ++ i ) cin >> lines[i];
        int ret = solve( lines );
        if ( ret == -1 )
            cout << "NA" << endl;
        else
            cout << ret << endl;
    }
	return 0;
}