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>
#include<set>
#include<string>
#include<vector>

#define MAX 40

#define WALL '#'
#define EXIT 'X'
#define TILE '.'

#define EAST 0
#define NORTH 1
#define WEST 2
#define SOUTH 3
#define MAXDIR 4

#define rep(i,b,n) for(int i=(b);i<(n);++i)

int condir[128];
char revdir[4];
const int di[] = {0,-1,0,1};
const int dj[] = {1,0,-1,0};

using namespace std;

typedef pair<int,int> pi;

struct Squares{
  int H,W;
  Squares():H(0),W(0){}
  Squares(int H, int W):H(H),W(W){}
  char D[MAX][MAX];

  bool isInside(int i, int j)const{
    return 0<=i&&i<H&&0<=j&&j<W;
  }
  bool isEmpty(int i, int j)const{
    return D[i][j] == EXIT || D[i][j] == TILE;
  }
  bool isPerson(int i, int j)const{
    return D[i][j] == 'E' || D[i][j] == 'N' || D[i][j] == 'W' || D[i][j] == 'S';
  }
  void escapersStage1(){
    rep(i,0,H){
      rep(j,0,W){
	if( isPerson(i,j) ){
	  int initk = condir[D[i][j]]-1+MAXDIR;
	  int endk = initk + MAXDIR;
	  for(int k = initk; k < endk; ++k){
	    int ti = i+di[k%MAXDIR];
	    int tj = j+dj[k%MAXDIR];
	    if( isInside(ti,tj) ){
	      if( isEmpty( ti, tj ) ){
		D[i][j] = revdir[k%MAXDIR];
		break;
	      }
	    }
	  }
	}
      }
    }
  }

  pi getfront(int i, int j, int dir){
    return make_pair(i+di[dir],j+dj[dir]);
  }
  
  void escapersStage2(){
    char newD[MAX][MAX];
    rep(i,0,MAX)rep(j,0,MAX)newD[i][j]=-1;
    rep(i,0,H){
      rep(j,0,W){
	if( isEmpty(i,j) ){
	  for(int k = 0; k < MAXDIR; ++k){
	    int ti = i+di[k];
	    int tj = j+dj[k];
	    if( isInside(ti,tj) ){
	      pi pfront = getfront(ti,tj,condir[D[ti][tj]]);
	      if( isPerson( ti, tj ) && pfront.first == i && pfront.second == j ){
		newD[i][j]=D[ti][tj];
		newD[ti][tj]=TILE;
		if(D[i][j]==EXIT)newD[i][j]=EXIT;
		break;
	      }
	    }
	  }
	}
      }
    }
    rep(i,0,H)rep(j,0,W){if(newD[i][j]==-1)newD[i][j]=D[i][j];}
    rep(i,0,H)rep(j,0,W){D[i][j]=newD[i][j];}
  }
  int getRemainedPersons(){
    int ret = 0;
    rep(i,0,H)rep(j,0,W){
      if( isPerson(i,j) )++ret;
    }
    return ret;
  }
  void Print(){
    cout << " THE SQUARES COND : " << endl;
    rep(i,0,H){rep(j,0,W){
	cout << D[i][j];}cout<<endl;}
  }
};

int main()
{
  revdir[EAST] = 'E';
  revdir[NORTH] = 'N';
  revdir[WEST] = 'W';
  revdir[SOUTH] = 'S';
  condir[(int)'E'] = EAST;
  condir[(int)'N'] = NORTH;
  condir[(int)'W'] = WEST;
  condir[(int)'S'] = SOUTH;

  while(true){
    int W,H;
    int t = 0;
    cin >> W >> H;

    if( W == 0 && H == 0 ) break;
    
    Squares sq(H,W);
    rep(i,0,H){
      rep(j,0,W){
	cin >> sq.D[i][j];
      }
    }
  
    while( t <= 180 && sq.getRemainedPersons() > 0 ){
      //sq.Print();
      sq.escapersStage1();
      sq.escapersStage2();
      ++t;
    }

    if( t > 180 ) cout << "NA" << endl;
    else cout << t << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int, int>P;

int w, h;
char s[30][31], b[30][31];
map<char, int>dir{ {'N',0},{'E',1},{'S',2},{'W',3} };
char rd[] = "NESW";
int dx[]{ -1,0,1,0 }, dy[]{ 0,1,0,-1 }, a[]{ 1,0,3,2 };
int main() {
	while (scanf("%d%d", &w, &h), h) {
		rep(i, h)scanf("%s", s[i]);
		for (int i = 0; i <= 180; i++) {
			bool ok = true;
			rep(i, h)rep(j, w)for (auto p : dir) {
				if (s[i][j] == p.first) { ok = false; break; }
			}
			if (ok) {
				printf("%d\n", i); goto g;
			}
			rep(i, h)rep(j, w) {
				if (dir.find(s[i][j]) == dir.end())continue;
				int bs = dir[s[i][j]];
				rep(k, 4) {
					int nx = i + dx[(bs + a[k]) % 4], ny = j + dy[(bs + a[k]) % 4];
					if (0 <= nx&&nx < h && 0 <= ny&&ny < w && (s[nx][ny] == '.' || s[nx][ny] == 'X')) {
						s[i][j] = rd[(bs + a[k]) % 4]; break;
					}
				}
			}
			rep(i, h)rep(j, w)b[i][j] = s[i][j];
			rep(i, h)rep(j, w) {
				if (s[i][j] != 'X'&&s[i][j] != '.')continue;
				rep(k, 4) {
					int nx = i + dx[a[k]], ny = j + dy[a[k]];
					if (0 <= nx&&nx < h && 0 <= ny&&ny < w&&dir.find(s[nx][ny]) != dir.end() && nx + dx[dir[s[nx][ny]]] == i&&ny + dy[dir[s[nx][ny]]] == j) {
						if (s[i][j] == 'X')b[nx][ny] = '.';
						else swap(b[i][j], b[nx][ny]);
						break;
					}
				}
			}
			rep(i, h)rep(j, w)s[i][j] = b[i][j];
		}
		puts("NA");
	g:;
	}
}