Problem: The huge maze The Squares has been newly completed in the famous theme park. Evacuation drills must be conducted under the guidance of the fire department, but the time required for the drills cannot be predicted due to the huge maze. Therefore, you decided to develop an evacuation drill simulator based on the following specifications.

As shown in Fig. 1, the giant maze is represented by W × H squares of horizontal W and vertical H. Each square is either a passage (white square), a wall (brown square), or an emergency exit (green square). The circles in the figure represent people, and the lowercase letters (E, W, S, N) in them represent the direction in which the person is facing (north, south, east, and west). The figure is drawn with the upward direction facing north.


<image>

Figure 1




People in the giant maze initially stand facing either north, south, east, or west. Each person attempts to move in 1-second increments at the same time, following the steps below.

1. Look at the right, front, left, and back squares in the direction you are currently facing, and turn to the first vacant aisle or emergency exit you find. If there is no such square, the direction will not change.
2. If the square in front of you is open and not in front of another person, move it. If there are multiple people with the same square in front of you, the selected one will move in the order of the people in that square, east, north, west, and south.



Those who arrive at the emergency exit after moving will evacuate safely and disappear from the maze.

Create a program that inputs the given huge maze and the location information of people and outputs the time when all people finish evacuating. If it takes more than 180 seconds to escape, output NA. Maze and person location information is given by the characters in rows H and columns W. The meaning of each character is as follows.

: Wall
.: Floor
X: Emergency exit
E: People facing east
N: People facing north
W: People facing west
S: People facing south


The boundary between the maze and the outside is either the wall # or the emergency exit X. In addition, there is always one or more people in the huge maze.



Input

A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:


W H
str1
str2
::
strH


The first line gives the horizontal size W of the maze and the vertical size H (1 ≤ W, H ≤ 30). The following H line is given the string stri (length W) that represents the i-th line of the maze.

The number of datasets does not exceed 50.

Output

For each input dataset, the time when all people finish evacuating is output on one line.

Examples

Input

10 3
##########
#E.......X
##########
4 4
####
#N.#
#..X
####
5 5
#####
#N..#
###.X
#S..#
#####
6 6
######
#..#X#
#.EE.#
####N#
#....#
######
8 8
##X#####
#....E.#
#####.##
#.#...##
#.W.#..#
#.#.N#.X
#X##.#.#
########
0 0


Output

8
NA
9
16
10


Input

10 3

E.......X

4 4

N.#
..X

5 5

N..#
.X
S..#

6 6

..#X#
.EE.#
N#
....#

8 8
X#####
....E.#
.##
.#...##
.W.#..#
.#.N#.X
X##.#.#

0 0


Output

8
NA
9
16
10

Solution (in CPP):

Solution:
#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
typedef pair<int,int> P;
typedef pair<int,bool> B;
typedef pair<P,B> PP;
int W,H,N;
char fie[33][33];
int mie[33][33];
int ine[33][33];
vector<PP> q;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
void prfie(){
  for(int i=0;i<(int)q.size();i++){
    cout << q[i].first.first << " "<<q[i].first.second<<" : "<<q[i].second.first<<endl; 
  }
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++)
      cout << mie[j][i] << " ";
    cout << endl;
  }
  cout << endl;
  getchar();
}
int solve(){
  int turn=0;
  while(turn<=180 && N>0){
    ++turn;
    //    prfie();
    memset(ine,-1,sizeof(ine));
    for(int i=0;i<(int)q.size();i++){
      if(q[i].second.second) continue;
      for(int j=0;j<4;j++){
	PP p = q[i];
	int nx=(p.first.first)+dx[(p.second.first+j)%4];
	int ny=-p.first.second+dy[(p.second.first+j)%4];
	if(fie[nx][ny]!='#' && mie[nx][ny]==0){
	  q[i].second.first=(p.second.first+j+3)%4;
	  mie[p.first.first][-p.first.second]=q[i].second.first+1;
	  int nw=q[i].second.first;
	  if(nw==2) ine[nx][ny]=nw;
	  else if(ine[nx][ny]==2){}
	  else if(nw==3) ine[nx][ny]=nw;
	  else if(ine[nx][ny]==3){}
	  else if(nw==0) ine[nx][ny]=nw;
	  else if(ine[nx][ny]==0){}
	  else ine[nx][ny]=nw;
	  //	  cout << q[i].first.first << " "<<q[i].first.second<<" : "<<q[i].second.first<<endl;   
	  break;
	}
      }
    }
    sort(q.rbegin(),q.rend());
    for(int i=0;i<(int)q.size();i++){
      if(q[i].second.second) continue;
      PP p = q[i];
      int nx=(p.first.first)+dx[(p.second.first+1)%4];
      int ny=-p.first.second+dy[(p.second.first+1)%4];
      if(!mie[nx][ny] && ine[nx][ny]==p.second.first){
	mie[p.first.first][-p.first.second]=0;
	if(fie[nx][ny]=='X'){
	  --N;
	  q[i].second.second=true;
	  continue;
	}
	mie[nx][ny]=p.second.first+1;
	q[i]=PP(P(nx,-ny),B(p.second.first,false));
      }
    }
    
  }
  q.clear();
  return turn;
}
int main(){
  while(1){
    cin >> W >> H;
    if(!W && !H) break;
    N=0;
    memset(mie,0,sizeof(mie));
    for(int i=0;i<H;i++){
      for(int j=0;j<W;j++){
	cin >>fie[j][i];
	if(fie[j][i]=='#')
	  mie[j][i]=9;
	if(fie[j][i]=='E'){
	  q.push_back(PP(P(j,-i),B(0,false)));
	  mie[j][i]=1;
	}else if(fie[j][i]=='N'){
	  q.push_back(PP(P(j,-i),B(1,false)));
	  mie[j][i]=2;
	}else if(fie[j][i]=='W'){
	  q.push_back(PP(P(j,-i),B(2,false)));
	  mie[j][i]=3;
	}else if(fie[j][i]=='S'){
	  q.push_back(PP(P(j,-i),B(3,false)));
	  mie[j][i]=4;
	}
      }
    }
    N=(int)q.size();
    int res=solve();
    if(res>180) cout <<"NA"<<endl;
    else cout << res << endl;
  }
}