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 <string>
#include <cstring>

using namespace std;
class Room{
private:
  int row, col;
  char **squares;
public:
  Room(int, int);
  ~Room();

  void print(ostream *);

  int getrow(){return row;}
  int getcol(){return col;}
  void setRoom(char c, int i, int j){squares[i][j] = c;}
  bool setdirection();
  void move();
};

ostream &operator<<(ostream &os, Room &r);

Room::Room(int a, int b){
  row = a;
  col = b;
  squares = new char*[row];
  for(int i = 0; i < row; i++){
    squares[i] = new char[col];
  }
}

Room::~Room(){
  for(int i = 0; i < row; i++){
    delete[] squares[i];
  }
  delete[] squares;
}

void Room::print(ostream *os){
  for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++){
      *os << squares[i][j];
    }
    *os << endl;
  }
}

ostream &operator<<(ostream &os, Room &r){
  r.print(&os);
  return os;
}

bool Room::setdirection(){
  bool hasHuman = false;
  for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++){
      switch(squares[i][j]){
        // ?????????????????????
        case 'E':
        hasHuman = true;
        if(squares[i+1][j] == '.' || squares[i+1][j] == 'X'){
          squares[i][j] = 'S';
          break;
        }
        if(squares[i][j+1] == '.' || squares[i][j+1] == 'X'){
          squares[i][j] = 'E';
          break;
        }
        if(squares[i-1][j] == '.' || squares[i-1][j] == 'X'){
          squares[i][j] = 'N';
          break;
        }
        if(squares[i][j-1] == '.' || squares[i][j-1] == 'X'){
          squares[i][j] = 'W';
          break;
        }
        break;
        case 'W':
        hasHuman = true;
        if(squares[i-1][j] == '.' || squares[i-1][j] == 'X'){
          squares[i][j] = 'N';
          break;
        }
        if(squares[i][j-1] == '.' || squares[i][j-1] == 'X'){
          squares[i][j] = 'W';
          break;
        }
        if(squares[i+1][j] == '.' || squares[i+1][j] == 'X'){
          squares[i][j] = 'S';
          break;
        }
        if(squares[i][j+1] == '.' || squares[i][j+1] == 'X'){
          squares[i][j] = 'E';
          break;
        }
        break;
        case 'N':
        hasHuman = true;
        if(squares[i][j+1] == '.' || squares[i][j+1] == 'X'){
          squares[i][j] = 'E';
          break;
        }
        if(squares[i-1][j] == '.' || squares[i-1][j] == 'X'){
          squares[i][j] = 'N';
          break;
        }
        if(squares[i][j-1] == '.' || squares[i][j-1] == 'X'){
          squares[i][j] = 'W';
          break;
        }
        if(squares[i+1][j] == '.' || squares[i+1][j] == 'X'){
          squares[i][j] = 'S';
          break;
        }
        break;
        case 'S':
        hasHuman = true;
        if(squares[i][j-1] == '.' || squares[i][j-1] == 'X'){
          squares[i][j] = 'W';
          break;
        }
        if(squares[i+1][j] == '.' || squares[i+1][j] == 'X'){
          squares[i][j] = 'S';
          break;
        }
        if(squares[i][j+1] == '.' || squares[i][j+1] == 'X'){
          squares[i][j] = 'E';
          break;
        }
        if(squares[i-1][j] == '.' || squares[i-1][j] == 'X'){
          squares[i][j] = 'N';
          break;
        }
        break;
      }
    }
  }
  return hasHuman;
}

void Room::move(){
  char **tmp = new char *[row];
  for(int i = 0; i < row; i++){
    tmp[i] = new char[col];
    for(int j = 0; j < col; j++){
      tmp[i][j] = squares[i][j];
    }
  }
  //W
  for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++){
      if(squares[i][j] == 'W'){
        if(squares[i][j-1] == '.'){
          tmp[i][j] = '.'; tmp[i][j-1] = 'W';
        }
        else if(squares[i][j-1] == 'X'){
          tmp[i][j] = '.';
        }
      }
    }
  }
  for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++){
      squares[i][j] = tmp[i][j];
    }
  }

  //S
  for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++){
      if(squares[i][j] == 'S'){
        if(squares[i+1][j] == '.'){
          tmp[i][j] = '.'; tmp[i+1][j] = 'S';
        }
        else if(squares[i+1][j] == 'X'){
          tmp[i][j] = '.';
        }
      }
    }
  }
  for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++){
      squares[i][j] = tmp[i][j];
    }
  }

  //E
  for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++){
      if(squares[i][j] == 'E'){
        if(squares[i][j+1] == '.'){
          tmp[i][j] = '.'; tmp[i][j+1] = 'E';
        }
        else if(squares[i][j+1] == 'X'){
          tmp[i][j] = '.';
        }
      }
    }
  }
  for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++){
      squares[i][j] = tmp[i][j];
    }
  }
  //N
  for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++){
      if(squares[i][j] == 'N'){
        if(squares[i-1][j] == '.'){
          tmp[i][j] = '.'; tmp[i-1][j] = 'N';
        }
        else if(squares[i-1][j] == 'X'){
          tmp[i][j] = '.';
        }
      }
    }
  }
  for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++){
      squares[i][j] = tmp[i][j];
    }
    delete[] tmp[i];
  }
  delete[] tmp;
}

int main(void){

  while(true){
    int row, col;
    cin >> col;
    cin >> row;
    if(col == 0 && row == 0)
      break;
    Room room(row, col);
    for(int i = 0; i < room.getrow(); i++){
      string tmp;
      cin >> tmp;
      for(int j = 0; j < room.getcol(); j++){
        room.setRoom(tmp[j], i, j);
      }
    }
    //cout << "read" << endl;
    //cout << room;

    int i;
    for(i = 0; i <= 180 && room.setdirection(); i++){
      //cout << "setdirection" << endl;
      //cout << room;
      room.move();
      //cout << "moved" << endl;
      //cout << room;
    }
    if(i > 180){
      cout << "NA" << endl;
    }
    else
    cout << i << endl;
  }
  return 0;
}