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 <vector>

using namespace std;

const int size = 31;

char map[size][size];
char map2[size][size];

struct Peep {
  int dir;
  int x;
  int y;
  Peep(int dir, int x, int y) : dir(dir), x(x), y(y) {}
};

int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

int getDir(char c) {
  return c == 'N' ? 1
      : c == 'E' ? 0
      : c == 'W' ? 2
      : c == 'S' ? 3
      : -1;
}
char dirs[] = {'E', 'N', 'W', 'S'};

int main()
{
  int w, h;
  while(cin >> w >> h, w || h) {
    vector<Peep> peeps;
    int numPeeps = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        char c;
        cin >> c;
        int dir = getDir(c);
        if(dir != -1) {
          peeps.push_back(Peep(dir, j, i));
          numPeeps++;
        }
        map[i][j] = c;
      }
    }
    int t;
    for (t = 0; t <= 180 && numPeeps > 0; t++) {
      for (int i = 1; i < h-1; i++) {
        for (int j = 1; j < w-1; j++) {
          int dir = getDir(map[i][j]);
          if (dir != -1) {
            for (int k = 0; k < 4; k++) {
              int nextX = j + dx[(dir+k+3)%4];
              int nextY = i + dy[(dir+k+3)%4];
              char nextC = map[nextY][nextX];
              if(nextC == '.' || nextC == 'X') {
                map[i][j] = dirs[(dir+k+3)%4];
//                cout << "acc:" << nextX << ", "<< nextY << ", " << map[i][j]<<endl;
                break;
              }
              else {
//                cout << "neg:" << nextX << ", "<< nextY << ", " << dirs[(dir+k+3)%4]<<endl;
              }
            }
          }
        }
      }
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          map2[i][j] = map[i][j];
        }
      }

      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          if (map[i][j] != '.' && map[i][j] != 'X') continue;
          for (int k = 0; k < 4; k++) {
            int neibX = j + dx[k];
            int neibY = i + dy[k];
            if (neibX < 0 || w <= neibX || neibY < 0 || h <= neibY) continue;
            char neibC = map[neibY][neibX];
            int dir = getDir(neibC);
            if (dir == (k+2)%4) {
              if (map[i][j] == 'X') {
                map2[neibY][neibX] = '.';
                numPeeps--;
              }
              else {
                map2[i][j] = dirs[dir];
                map2[neibY][neibX] = '.';
              }
              break;
            }
          }
        }
      }
/*      for (int i = 0; i < h; i++) {
        cout << "bf:" << map[i] << endl;
      }
*/      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          map[i][j] = map2[i][j];
        }
//        cout << "af:" << map[i] << endl;
      }
//      cout << endl;
    }
    if (t <= 180) cout << t << endl;
    else cout << "NA" << endl;

  }
  return 0;
}