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++: #define _USE_MATH_DEFINES
#define INF 10000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

static const double eps = 1e-8;

struct DATA{
	char dirPrev;
	char dirNext;
	P prev;
	P next;
};

char stage[30][30];

static const int moveTo[][2] = {{1,0},{0,-1},{-1,0},{0,1}};
//static const char dir[] = {'N','W','S','E'};


void changeDir(int W,int H){
	map<char,int> dir;
	dir['N'] = 0; dir['W'] = 1; dir['S'] = 2; dir['E'] = 3;
	const char num2dir[] = {'E','N','W','S'};
	//# Ç
	//.(sIh)°
	//X ñíû
	//E ðü¢Ä¢él
	//N kðü¢Ä¢él
	//W ¼ðü¢Ä¢él
	//S ìðü¢Ä¢él

	for(int y=0;y<H;y++){
		for(int x=0;x<W;x++){
			switch(stage[y][x]){
			case '#':			  
				break;
			case 'X':
				break;
			case 'N': case 'W': case 'S': case 'E':
				for(int i=0;i<4;i++){
					int dx = x + moveTo[(i+dir[stage[y][x]])%4][0];
					int dy = y + moveTo[(i+dir[stage[y][x]])%4][1];
					if(dy < 0 || dy >= H || dx < 0 || dx >= W) continue;
					if(stage[dy][dx] == '.' || stage[dy][dx] == 'X'){
						stage[y][x] = num2dir[(i+dir[stage[y][x]])%4];
						break;
					}
				}

				break;
			default :
				break;
			}

		}
	}
}

void movePlayer(int W,int H){
	map<char,int> dir;
	dir['N'] = 0; dir['W'] = 1; dir['S'] = 2; dir['E'] = 3;
	const char num2other_dir[] = {'W','S','E','N'};
	//# Ç
	//.(sIh)°
	//X ñíû
	//E ðü¢Ä¢él
	//N kðü¢Ä¢él
	//W ¼ðü¢Ä¢él
	//S ìðü¢Ä¢él

	bool used[30][30];
	fill((bool*)used,(bool*)used+30*30,false);
	for(int dy=0;dy<H;dy++){
		for(int dx=0;dx<W;dx++){
			
			switch(stage[dy][dx]){
			case '#':			  
				break;
			case 'X':
				for(int i=0;i<4;i++){
					int sx = dx + moveTo[i][0];
					int sy = dy + moveTo[i][1];
					
					if(sy < 0 || sy >= H || sx < 0 || sx >= W) continue;
					if(used[sy][sx]) continue;
					if(stage[sy][sx] == num2other_dir[i]){
						stage[sy][sx] = '.';
						used[dy][dx] = true;
						used[sy][sx] = true;
						break;
					}
				}
				break;
			case '.':
				for(int i=0;i<4;i++){
					int sx = dx + moveTo[i][0];
					int sy = dy + moveTo[i][1];
					
					if(sy < 0 || sy >= H || sx < 0 || sx >= W) continue;
					if(used[sy][sx]) continue;
					if(stage[sy][sx] == num2other_dir[i]){
						stage[dy][dx] = num2other_dir[i];
						stage[sy][sx] = '.'; 
						used[dy][dx] = true;
						used[sy][sx] = true;
						break;
					}
				}
				break;
			case 'N': case 'W': case 'S': case 'E':
				break;
			default :
				break;
			}

		}
	}
}

void printStage(int W,int H){
	for(int y=0;y<H;y++){
		for(int x=0;x<W;x++){
			printf("%c",stage[y][x]);
		}
		printf("\n");
	}
}

bool isClear(int W,int H){
	for(int y=0;y<H;y++){
		for(int x=0;x<W;x++){
			switch(stage[y][x]){
			case 'N': case 'W': case 'S': case 'E':
				return false;
				break;
			default:
				break;
			}
		}
	}
	return true;
}

int main(){
	int W,H;
	while(~scanf("%d %d",&W,&H)){
		if(W==H && H==0) break;
		memset(stage,'#',sizeof(stage));
		for(int y=0;y<H;y++){
			char buf[32];
			scanf("%s",buf);
			for(int x=0;x<W;x++){
				stage[y][x] = buf[x];
			}
		}

		int res=0;
		//printStage(W,H);
		while(!isClear(W,H)){
			
 			changeDir(W,H);
			//printStage(W,H);
			movePlayer(W,H);
			//printStage(W,H);
			res++;
			if(res > 180) break;
		}

		if(res > 180) printf("NA\n");
		else printf("%d\n",res);

	}

}

C++: #include<iostream>
using namespace std;
int dy[]={-1,0,1,0};
int dx[]={0,1,0,-1};
int change(char ch){
  if(ch=='N')return 0;
  if(ch=='E')return 1;
  if(ch=='S')return 2;
  if(ch=='W')return 3;
  return -1;
}
char change2(int x){
  if(x==0)return 'N';
  if(x==1)return 'E';
  if(x==2)return 'S';
  if(x==3)return 'W';
  return '-';
}
char t[30][30],tmp[30][30];
int W,H;

int main(){
  while(1){
    cin>>W>>H;
    if(W==0&&H==0)break;
    for(int i=0;i<H;i++)
      for(int j=0;j<W;j++)
	cin>>t[i][j];
    int ans=0;
    while(1){
      ///////////////////////////////////////////////
      bool flag=false;
      for(int i=0;i<H;i++){
	for(int j=0;j<W;j++){
	  if(change(t[i][j])==-1)continue;
	  flag=true;
	  int dir=change(t[i][j]);
	  for(int k=5;k>=2;k--){
	    int ndir=(dir+k)%4;
	    int ny=i+dy[ndir],nx=j+dx[ndir];
	    if(t[ny][nx]=='.'||t[ny][nx]=='X'){
	      t[i][j]=change2(ndir);
	      break;
	    }
	  }//k
	}//j
      }//i
      if(flag==false)break;
      ///////////////////////////////////////////////
      ans++;
      if(ans>180)break;
      /////////////////////////////////////////////// w s e n

      for(int i=0;i<H;i++){
	for(int j=0;j<W;j++){
	  if(change(t[i][j])==-1){
	    tmp[i][j]=t[i][j];
	  }else tmp[i][j]='.';
	}
      }

      for(int k=3;k>=0;k--){
	for(int i=0;i<H;i++){
	  for(int j=0;j<W;j++){
	    if(k!=change(t[i][j]))continue;
	    int ny=i+dy[k],nx=j+dx[k];
	    if(tmp[ny][nx]=='.'||tmp[ny][nx]=='X'){
	      if(tmp[ny][nx]=='.')tmp[ny][nx]=t[i][j];
	    }else{
	      tmp[i][j]=t[i][j];
	    }
	  }
	}
      }

      for(int i=0;i<H;i++){
	for(int j=0;j<W;j++){
	  t[i][j]=tmp[i][j];
	}
      }
      ///////////////////////////////////////////////
      
    }
    if(ans>180)cout<<"NA"<<endl;
    else cout<<ans<<endl;
  }
  return 0;
}