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<bits/stdc++.h>
#define N 30
using namespace std;
int dy[4]={0,-1,0,1};
int dx[4]={1,0,-1,0};
int w,h,ans,cnt,sum;
bool used[N][N];
string s[N];


void Change_dir(){
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(!('0'<=s[i][j]&&s[i][j]<='3'))continue;
      int dir=s[i][j]-'0';
      for(int k=3;k<=6;k++){
	int ndir=(dir+k)%4;
	int ny=i+dy[ndir],nx=j+dx[ndir];
	if(ny<0||nx<0||h<=ny||w<=nx)continue;
	if(s[ny][nx]=='.'||s[ny][nx]=='X'){
	  s[i][j]=ndir+'0';
	  break;
	}
      }
    }
  }
}

void Change_pls(){
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(!(s[i][j]=='.'||s[i][j]=='X'))continue;
      if(used[i][j])continue;
      for(int k=0;k<4;k++){
	int ny=i+dy[k],nx=j+dx[k];
	if(ny<0||nx<0||h<=ny||w<=nx)continue;
	if(used[ny][nx])continue;
	if('0'<=s[ny][nx]&&s[ny][nx]<='3'){
	  if(s[ny][nx]!=(k+2)%4+'0')continue;
	  used[i][j]=used[ny][nx]=true;
	  if(s[i][j]=='.')s[i][j]=s[ny][nx];
	  else cnt++;
	  s[ny][nx]='.';
	  break;
	}
      }
    }
  }
}

int main(){
  while(1){
    cin>>w>>h;
    if(!w&&!h)break;
    ans=cnt=sum=0;
    for(int i=0;i<h;i++)cin>>s[i];
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++){
	if(s[i][j]=='E')s[i][j]='0',sum++;
	if(s[i][j]=='N')s[i][j]='1',sum++;
	if(s[i][j]=='W')s[i][j]='2',sum++;
	if(s[i][j]=='S')s[i][j]='3',sum++;
      }
    while(1){
      ans++;
      if(ans>180)break;
      Change_dir();
      memset(used,0,sizeof(used));
      Change_pls();
      if(cnt==sum)break;
    }
    if(ans>180)cout<<"NA"<<endl;
    else cout<<ans<<endl;
  }
  return 0;
}