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<iostream>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)

//right+1,front+0,left+3,back+2
enum{NORTH,EAST,SOUTH,WEST};
const int dir[4]={1,0,3,2};
const int dx[]={0,1,0,-1};
const int dy[]={-1,0,1,0};

const int gdir[]={WEST,SOUTH,EAST,NORTH};
const int gx[]  ={1,0,-1,0};
const int gy[]  ={0,-1,0,1};


const int N = 100;
char inp[N][N];
//-1 daremo inai
int m[N][N];//-1,  dir 0,1,2,3 
int mnext[N][N];

void output(int r,int c){
  rep(i,r){
    rep(j,c){
      cout << m[i][j]<<" " ;
    }
    cout << endl;
  }
  cout << endl;
}

void solve(int r,int c){
  rep(loop,181){
    rep(i,r)rep(j,c)if (m[i][j] != -1)goto end;
    cout << loop << endl;
    return;
  end:;

    //change dir
    rep(i,r){
      rep(j,c){
	if (m[i][j] == -1)continue;
	rep(k,4){
	  int next = (m[i][j] + dir[k])%4;
	  int nex = j+dx[next],ney=i+dy[next];
	  if (nex == -1 || ney == -1 || nex == c || ney == r)continue;
	  if (inp[ney][nex] != '#' && m[ney][nex] == -1){
	    m[i][j] = next;
	    break;
	  }
	}
      }
    }

    rep(i,r)rep(j,c)mnext[i][j] = m[i][j];
    //move
    rep(i,r){
      rep(j,c){
	if (inp[i][j] == '#' || m[i][j] != -1)continue;
	rep(k,4){
	  int ney = i+gy[k],nex=j+gx[k];
	  if (nex == -1 || ney == -1 || nex == c || ney == r)continue;
	  //cout << i <<" " << j <<" " << ney <<" " << nex <<" " << m[ney][nex] << " " << k << " " << gdir[k] << endl;
	  if (m[ney][nex] == gdir[k]){
	    swap(mnext[i][j],mnext[ney][nex]);
	    break;
	  }
	}
      }
    }

    rep(i,r)rep(j,c)m[i][j] = mnext[i][j];
    //output(r,c);
    rep(i,r)rep(j,c)if (inp[i][j] == 'X' && m[i][j] != -1)m[i][j] = -1;
  }
  cout << "NA" << endl;
}

main(){
  int c,r;
  while(cin>>c>>r && c){
    rep(i,r)cin>>inp[i];
    rep(i,r){
      rep(j,c){
	if (inp[i][j] == '.' || inp[i][j] == '#' || inp[i][j] == 'X'){
	  m[i][j] = -1;
	}else if (inp[i][j] == 'E'){
	  inp[i][j] = '.';
	  m[i][j] = 1;
	}else if (inp[i][j] == 'S'){
	  inp[i][j] = '.';
	  m[i][j] = 2;
	}else if (inp[i][j] == 'W'){
	  inp[i][j] = '.';
	  m[i][j] = 3;
	}else if (inp[i][j] == 'N'){
	  inp[i][j] = '.';
	  m[i][j] = 0;
	}
      }
    }
    solve(r,c);
  }
}

C++: // 2011/08/05 Tazoe

#include <iostream>
using namespace std;

struct man{
	int x;
	int y;
	int d;
};

int main()
{
	while(true){
		int W, H;
		cin >> W >> H;
		if(W==0&&H==0)
			break;

		char S[30][30];
		struct man M[900];
		int N = 0;
		for(int y=0; y<H; y++){
			for(int x=0; x<W; x++){
				cin >> S[y][x];

				if(S[y][x]=='E'||S[y][x]=='N'||S[y][x]=='W'||S[y][x]=='S'){
					M[N].x = x;
					M[N].y = y;
					if(S[y][x]=='E')
						M[N].d = 2;
					else if(S[y][x]=='N')
						M[N].d = 3;
					else if(S[y][x]=='W')
						M[N].d = 0;
					else if(S[y][x]=='S')
						M[N].d = 1;
					N++;
					S[y][x] = '@';
				}
			}
		}

		bool flg = false;
		for(int t=1; t<=180; t++){
			for(int i=0; i<N; i++){
				int D = (M[i].d+4-1)%4;
				for(int j=0; j<4; j++){
					if(D==2&&(S[M[i].y][M[i].x+1]=='.'||S[M[i].y][M[i].x+1]=='X'))
						break;
					else if(D==3&&(S[M[i].y-1][M[i].x]=='.'||S[M[i].y-1][M[i].x]=='X'))
						break;
					else if(D==0&&(S[M[i].y][M[i].x-1]=='.'||S[M[i].y][M[i].x-1]=='X'))
						break;
					else if(D==1&&(S[M[i].y+1][M[i].x]=='.'||S[M[i].y+1][M[i].x]=='X'))
						break;
					if(j<3)
						D = (D+1)%4;
					else
						D = M[i].d;
				}
				M[i].d = D;
			}

			for(int i=0; i<N-1; i++)
				for(int j=i+1; j<N; j++)
					if(M[i].d>M[j].d){
						struct man tmp = M[i];
						M[i] = M[j];
						M[j] = tmp;
					}

			for(int i=0; i<N; i++)
				if(M[i].d==2&&(S[M[i].y][M[i].x+1]=='.'||S[M[i].y][M[i].x+1]=='X')){
					if(S[M[i].y][M[i].x+1]=='X')
						M[i].d = 9;
					S[M[i].y][M[i].x] = '.';
					M[i].x += 1;
					S[M[i].y][M[i].x] = '@';
				}
				else if(M[i].d==3&&(S[M[i].y-1][M[i].x]=='.'||S[M[i].y-1][M[i].x]=='X')){
					if(S[M[i].y-1][M[i].x]=='X')
						M[i].d = 9;
					S[M[i].y][M[i].x] = '.';
					M[i].y -= 1;
					S[M[i].y][M[i].x] = '@';
				}
				else if(M[i].d==0&&(S[M[i].y][M[i].x-1]=='.'||S[M[i].y][M[i].x-1]=='X')){
					if(S[M[i].y][M[i].x-1]=='X')
						M[i].d = 9;
					S[M[i].y][M[i].x] = '.';
					M[i].x -= 1;
					S[M[i].y][M[i].x] = '@';
				}
				else if(M[i].d==1&&(S[M[i].y+1][M[i].x]=='.'||S[M[i].y+1][M[i].x]=='X')){
					if(S[M[i].y+1][M[i].x]=='X')
						M[i].d = 9;
					S[M[i].y][M[i].x] = '.';
					M[i].y += 1;
					S[M[i].y][M[i].x] = '@';
				}

			for(int i=0; i<N; i++)
				if(M[i].d==9)
					S[M[i].y][M[i].x] = 'X';

			for(int i=0; i<N-1; i++)
				for(int j=i+1; j<N; j++)
					if(M[i].d>M[j].d){
						struct man tmp = M[i];
						M[i] = M[j];
						M[j] = tmp;
					}

			for(int i=0; i<N; i++)
				if(M[i].d==9){
					N = i;
					break;
				}
/*
			char S2[30][30];
			for(int y=0; y<H; y++)
				for(int x=0; x<W; x++)
					S2[y][x] = S[y][x];

			for(int i=0; i<N; i++)
				if(M[i].d==2)
					S2[M[i].y][M[i].x] = 'E';
				else if(M[i].d==3)
					S2[M[i].y][M[i].x] = 'N';
				else if(M[i].d==0)
					S2[M[i].y][M[i].x] = 'W';
				else if(M[i].d==1)
					S2[M[i].y][M[i].x] = 'S';

			for(int y=0; y<H; y++){
				for(int x=0; x<W; x++)
					cout << S2[y][x];
				cout << endl;
			}
			break;
*/
			if(N==0){
				cout << t << endl;
				flg = true;
				break;
			}
		}

		if(!flg)
			cout << "NA" << endl;
	}

	return 0;
}