#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit
#include <cstdio>	// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define EAST 0
#define NORTH 1
#define WEST 2
#define SOUTH 3
#define DEBUG 0

using namespace std;

typedef pair<int, int> P;	
/*
typedef pair<P, P> PP;ツ　// ツ個サツ催敖つ「ツづゥツ暗環置, ツ篠淞づ個暗環置  Pi (P (crow, ccol ), P (nr, nc ) )
typedef pair<PP, int> PPi; // ツ個サツ催敖づ個暗環置ツ、ツ篠淞づ個暗環置ツ、ツ個サツ催敖古シツつ「ツづつ「ツづゥツづ個陛サツ古シ
*/
struct Person{
	int nrow, ncol;
	int dir;
	bool canmove;
};
const int timeout = 180;// default: 180;
int w, h;
char mize[32][32];

char print_dir (int n )
{
	const char dir[4] = { 'E', 'N', 'W', 'S' };
	return dir[n]; 	
}

void disp_dir (int n )
{
	const string dir[4] = { "EAST", "NORTH", "WEST", "SOUTH" };
	cout << "dir: " << dir[n] << endl;
}

void disp_mize (void )
{
	rep (i, h ){
		rep (j, w ){
			cout << mize[i][j];
		} // end rep
		cout << endl;
	} // end rep
	cout << endl;
}

int main()
{
	const int dir[][2] = { { 0, 1 }, {-1, 0 }, { 0, -1 }, { 1, 0 }  }; // EAST, NORTH, WEST, SOUTH order
//	cut here before submit 
//	freopen ("testcase.Square", "r", stdin );
	map<char, int> Dir;
	Dir['N'] = NORTH, Dir['E'] = EAST, Dir['S'] = SOUTH, Dir['W'] = WEST; 
	while (scanf ("%d %d", &w, &h ) && w && h ){
		memset (mize, 0, sizeof (mize ) );
		set<P> ee;	// ツ氾アツ湘ュツ古サ
		ee.clear();
		rep (i, h ){
			rep (j, w ){
				scanf (" %c", mize[i]+j );
				if (mize[i][j] == 'X' )
					ee.insert (P (i, j ) );
			} // end rep
		} // end rep
		int res = 0;
		bool escape = false;
		while (res <= timeout ){ 
#if DEBUG
			disp_mize();
#endif
			map<P,Person> person;
			person.clear();
//			int id = 0;
			rep (i, h ){
				rep (j, w ) {
					if (isalpha (mize[i][j] ) && mize[i][j] != 'X' ){
						Person p;
						p.nrow = -1, p.ncol = -1, p.dir = Dir[mize[i][j]];
						p.canmove = false;
						person[P(i, j )] = p;
//						mize[i][j] = '.';
					} // end if
				} // end rep
			} // end rep
			if (person.empty() ){
				escape = true;
				break;
			} // end if
			map<P,Person>::iterator it = person.begin();
			for (; it != person.end(); it++ ){
				P pp = (*it).first;
				Person p = (*it).second;
				int crow = pp.first;
				int ccol = pp.second;
				int cdir = p.dir;
				bool ne = false;
				for (int k = (4 + cdir - 1) % 4, cnt = 0;cnt < 4; k = (k + 1 ) % 4, cnt++ ){
					int nr = crow + dir[k][0];
					int nc = ccol + dir[k][1];
					if (nr < 0 || nc < 0 || nr >= h || nc >= w || mize[nr][nc] == '#' ) continue;
					if (isalpha(mize[nr][nc] ) && mize[nr][nc] != 'X' ) continue;
					Person nd;
					nd.nrow = nr, nd.ncol = nc, nd.dir = k;
					nd.canmove = true;
					person[P(crow, ccol)] = nd;
					ne = true;
					break;
				} // end for
				if (!ne ){
					Person nd;
					nd.nrow = crow, nd.ncol = ccol, nd.dir = cdir;
					nd.canmove = false;
					person[P(crow, ccol) ] = nd;
				} // end if
			} // end rep
			set<P> next;
			next.clear();
			for (it = person.begin(); it != person.end(); it++ ){
				Person cp = (*it).second;
				next.insert (P (cp.nrow, cp.ncol ) );
			} // end rep
			if (!next.empty() ){
				set<P>::iterator jt = next.begin();
				for (; jt != next.end(); jt++ ){
					int crow = (*jt).first;
					int ccol = (*jt).second;
					bool can = true;
					rep (k, 4 ){
						int nr = crow + dir[k][0];
						int nc = ccol + dir[k][1];
						if (nr < 0 || nc < 0 || nr >= h || nc >= w || mize[nr][nc] == '#' || mize[nr][nc] == '.' || mize[nr][nc] == 'X' ) continue;
						if (can ){
							person[P (nr, nc )].canmove = true;
							can = false;
						}else{
							person[P (nr, nc )].canmove = false;
						} // end if
					} // end rep
				} // end rep
			} // end if
			for (it = person.begin(); it != person.end(); it++ ){
				P pp = (*it).first;
				Person p = (*it).second;
				if (p.canmove ){
					if (ee.count (P (p.nrow, p.ncol ) ) ){
						mize[pp.first][pp.second] = '.';
						continue;
					} // end if
					mize[pp.first][pp.second] = '.';
					mize[p.nrow][p.ncol] = print_dir (p.dir );				
//				}else{
//					mize[pp.first][pp.second] = '.';
//					mize[pp.first][pp.second] = print_dir (p.dir );
				} // end if
#if DEBUG
				cout << '(' << pp.first << ',' << pp.second << ')' << endl;
				disp_dir (p.dir );
				cout << "next_pos: (" << p.nrow << ',' << p.ncol << ')' << endl;
				cout << "can_move: " << (p.canmove ? "YES" : "NO" ) << endl;
#endif

			} // end for
			res++;
		} // end while
		if (escape ){
			printf ("%d\n", res );
		}else{
			printf ("NA\n" );
		} // end if
	} // end loop

//	printf ("%d\n", res );
		
	return 0;
}