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 <iostream>
#include <vector>
#include <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>

enum class Direction {
	East, North, West, South
};
enum class RelationalDirection {
	Right, Forward, Left, Backward
};
constexpr std::array<Direction, 4> Directions{ Direction::East, Direction::North, Direction::West, Direction::South };
constexpr std::array<RelationalDirection, 4> RelationalDirestions{ RelationalDirection::Right, RelationalDirection::Forward, RelationalDirection::Left, RelationalDirection::Backward };

int to_int(Direction dir) {
	switch (dir) {
	case Direction::East: return 0;
	case Direction::North: return 1;
	case Direction::West: return 2;
	case Direction::South: return 3;
	default: throw 0;
	}
}
int to_int(RelationalDirection dir) {
	switch (dir) {
	case RelationalDirection::Right: return 3;
	case RelationalDirection::Forward: return 0;
	case RelationalDirection::Left: return 1;
	case RelationalDirection::Backward: return 2;
	default: throw 0;
	}
}

Direction operator+(Direction dir, RelationalDirection rdir) {
	return Directions[(to_int(dir) + to_int(rdir)) % 4];
}
Direction operator+(RelationalDirection rdir, Direction dir) {
	return Directions[(to_int(rdir) + to_int(dir)) % 4];
}

struct Coordinate {
	int x, y;
};
Coordinate from_dir(Direction dir) {
	switch (dir) {
	case Direction::East: return Coordinate{ 1, 0 };
	case Direction::North: return Coordinate{ 0, -1 };
	case Direction::West: return Coordinate{ -1, 0 };
	case Direction::South: return Coordinate{ 0, 1 };
	default: throw 0;
	}
}
Coordinate operator+(Coordinate a, Coordinate b) {
	return Coordinate{ a.x + b.x, a.y + b.y };
}
Coordinate operator+(Coordinate a, Direction dir) {
	return a + from_dir(dir);
}
Coordinate operator+(Direction dir, Coordinate a) {
	return from_dir(dir) + a;
}
bool operator==(Coordinate a, Coordinate b) {
	return a.x == b.x && a.y == b.y;
}

struct Person {
	Coordinate position;
	Direction dir;
};

int main() {
	while (true) {
		int width, height; std::cin >> width >> height; if (width == 0 && height == 0) break;
		std::vector<std::string> state(height); for (auto& line : state) std::cin >> line;
		std::vector<Person> person;
		for (auto i = 0; i < height; ++i) for (auto j = 0; j < width; ++j) {
			switch (state[i][j]) {
			case 'E': person.push_back(Person{ Coordinate{j, i}, Direction::East }); break;
			case 'N': person.push_back(Person{ Coordinate{j, i}, Direction::North }); break;
			case 'W': person.push_back(Person{ Coordinate{j, i}, Direction::West }); break;
			case 'S': person.push_back(Person{ Coordinate{j, i}, Direction::South }); break;
			default: break;
			}
		}
		std::vector<Coordinate> next_pos(person.size());
		std::vector<std::vector<int>> id(height, std::vector<int>(width, -1));
		std::vector<std::vector<bool>> is_in_queue(height, std::vector<bool>(width, false));
		std::queue<int> in_maze; for (auto i = 0; i < person.size(); ++i) {
			in_maze.push(i);
			id[person[i].position.y][person[i].position.x] = i;
		}
		std::queue<Coordinate> move_target;
		int count = -1;
		for (; count <= 180 && !in_maze.empty(); ++count) {
			for (auto c = in_maze.size(); c > 0; --c) {
				auto top = in_maze.front(); in_maze.pop();
				if (state[person[top].position.y][person[top].position.x] == 'X') continue;
				for (const auto dir : RelationalDirestions) {
					auto next = person[top].position + (person[top].dir + dir);
					if (state[next.y][next.x] != '#' && id[next.y][next.x] == -1) {
						if (!is_in_queue[next.y][next.x]) {
							is_in_queue[next.y][next.x] = true;
							move_target.push(next);
						}
						person[top].dir = person[top].dir + dir;
						next_pos[top] = next;
						break;
					}
				}
				in_maze.push(top);
			}
			while (!move_target.empty()) {
				auto top = move_target.front(); move_target.pop();
				is_in_queue[top.y][top.x] = false;
				for (const auto dir : Directions) {
					auto suspect = top + dir;
					if (0 <= suspect.y && suspect.y < height && 0 <= suspect.x && suspect.x < width && id[suspect.y][suspect.x] != -1 && next_pos[id[suspect.y][suspect.x]] == top) {
						if (state[top.y][top.x] != 'X') {
							id[top.y][top.x] = id[suspect.y][suspect.x];
						}
						person[id[suspect.y][suspect.x]].position = top;
						id[suspect.y][suspect.x] = -1;
						break;
					}
				}
			}
		}
		if (count > 180) {
			std::cout << "NA\n";
		}
		else {
			std::cout << count << '\n';
		}
	}
}