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 <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <unordered_set>
//#include <unordered_map>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;


int h,w;
stringstream ss;
 









int dy[4][4]={{1,0,-1,0},
			  {0,-1,0,1},
			  {-1,0,1,0},
			  {0,1,0,-1}};
int dx[4][4]={{0,1,0,-1},
			  {1,0,-1,0},
			  {0,-1,0,1},
			  {-1,0,1,0}};
int dd[4][4]={{4,1,2,3},
			  {1,2,3,4},
			  {2,3,4,1},
			  {3,4,1,2}};
int dy2[4]={0,1,0,-1};
int dx2[4]={-1,0,1,0};

void mainmain(){
	while(cin>>w>>h,h||w){
		vvint vv;
		initvv(vv,h+2,w+2,-1);
		int n=0;
		reep(i,1,h+1){
			string s;
			cin>>s;
			reep(j,1,w+1){
				char t=s[j-1];
				// maps[i-1][j-1]=t;
				if(t=='#'){
					vv[i][j]=-1;
				}
				else if(t=='.'){
					vv[i][j]=0;
				}
				else if(t=='X'){
					vv[i][j]=-2;
				}
				else if(t=='E'){
					vv[i][j]=1;
					n++;
				}
				else if(t=='N'){
					vv[i][j]=2;
					n++;
				}
				else if(t=='W'){
					vv[i][j]=3;
					n++;
				}
				else if(t=='S'){
					vv[i][j]=4;
					n++;
				}
			}
		}
		// if(cc==204){
			// cout<<w<<" "<<h<<" "<<endl;
			// rep(i,h){
				// rep(j,w){
					// cout<<maps[i][j];
				// }
				// cout<<endl;
			// }
			// cout<<0<<" "<<0<<endl;
		// }
		// continue;
		vvint used;
		initvv(used,h+2,w+2);
		bool f=false;
		int cnt=0;
		rep(i,180){
			// if(i>10) break;
			// rep(k,vv.size()){
				// rep(j,vv[0].size()){
					// printf("%2d",vv[k][j]);
				// }
				// cout<<endl;
			// }
			// cout<<endl;
			rep(k,h+2){
				rep(j,w+2){
					used[k][j]=0;
				}
			}
			reep(j,1,h+1){
				reep(k,1,w+1){
					if(vv[j][k]>=1){
						rep(l,4){
							int y=j+dy[vv[j][k]-1][l];
							int x=k+dx[vv[j][k]-1][l];
							if(1<=y&&y<h+1&&1<=x&&x<w+1){
								if(vv[y][x]==0||vv[y][x]==-2){
									vv[j][k]=dd[vv[j][k]-1][l];
									break;
								}
							}
						}
					}
				}
			}
			int o=0;
				reep(j,1,h+1){
					reep(k,1,w+1){
						if(vv[j][k]==(o+2)%4+1){
							int y=j+dy2[o];
							int x=k+dx2[o];
							if(1<=y&&y<h+1&&1<=x&&x<w+1);
							else continue; 
							// cout<<j<<" "<<k<<" "<<dy2[o]<<" "<<dx2[o]<<endl;
							// cout<<y<<" "<<x<<endl;
							if(vv[y][x]==-2&&used[y][x]==0&&used[j][k]==0){
								cnt++;
								vv[j][k]=0;
								used[y][x]=1;
								used[j][k]=1;
							}
							else if(vv[y][x]==0&&used[j][k]==0&&used[y][k]==0){
								used[j][k]=1;
								vv[j][k]=0;
								vv[y][x]=(o+2)%4+1;
								used[y][x]=1;
							}
						}
					}
				}
			o=1;
				for(int j=h;j>=1;j--){
					reep(k,1,w+1){
						if(vv[j][k]==(o+2)%4+1){
							int y=j+dy2[o];
							int x=k+dx2[o];
							if(1<=y&&y<h+1&&1<=x&&x<w+1);
							else continue; 
							// cout<<j<<" "<<k<<" "<<dy2[o]<<" "<<dx2[o]<<endl;
							// cout<<y<<" "<<x<<endl;
							if(vv[y][x]==-2&&used[y][x]==0&&used[j][k]==0){
								cnt++;
								vv[j][k]=0;
								used[y][x]=1;
								used[j][k]=1;
							}
							else if(vv[y][x]==0&&used[j][k]==0&&used[y][x]==0){
								vv[j][k]=0;
								vv[y][x]=(o+2)%4+1;
								used[y][x]=1;
								used[j][k]=1;
							}
						}
					}
				}
			o=2;
				reep(j,1,h+1){
					for(int k=w;k>=1;k--){
						if(vv[j][k]==(o+2)%4+1){
							int y=j+dy2[o];
							int x=k+dx2[o];
							if(1<=y&&y<h+1&&1<=x&&x<w+1);
							else continue; 
							// cout<<j<<" "<<k<<" "<<dy2[o]<<" "<<dx2[o]<<endl;
							// cout<<y<<" "<<x<<endl;
							if(vv[y][x]==-2&&used[y][x]==0&&used[j][k]==0){
								cnt++;
								vv[j][k]=0;
								used[y][x]=1;
								used[j][k]=1;
							}
							else if(vv[y][x]==0&&used[j][k]==0&&used[y][x]==0){
								vv[j][k]=0;
								vv[y][x]=(o+2)%4+1;
								used[y][x]=1;
								used[j][k]=1;
							}
						}
					}
				}
			o=3;
				reep(j,1,h+1){
					reep(k,1,w+1){
						if(vv[j][k]==(o+2)%4+1){
							int y=j+dy2[o];
							int x=k+dx2[o];
							if(1<=y&&y<h+1&&1<=x&&x<w+1);
							else continue; 
							// cout<<j<<" "<<k<<" "<<dy2[o]<<" "<<dx2[o]<<endl;
							// cout<<y<<" "<<x<<endl;
							if(vv[y][x]==-2&&used[y][x]==0&&used[j][k]==0){
								cnt++;
								vv[j][k]=0;
								used[y][x]=1;
								used[j][k]=1;
							}
							else if(vv[y][x]==0&&used[j][k]==0&&used[y][x]==0){
								vv[j][k]=0;
								vv[y][x]=(o+2)%4+1;
								used[j][k]=1;
								used[y][x]=1;
							}
						}
					}
				}
			if(cnt==n){
				ss<<i+1<<endl;
				f=true;
				break;
			}
		}
		if(!f){
			ss<<"NA\n";
		}
		string s1;
		ss>>s1;
		cout<<s1<<endl;
		// cout<<s1<<" "<<s2<<endl;
	}
}



}
main() try{
    mainmain();
}
catch(...){
}