Language: C++

#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF (1<<29)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))
#define fr first
#define sc second
typedef long long int ll;
const int dx[] = {-1,0,1,0},dy[] = {0,1,0,-1};
const int ddir[] = {3,0,1,2};

const char dir[] = {'W','S','E','N'};


using namespace std;

struct person{
    int dir;
    int x;
    int y;
};

void Sort(vector< struct person> &p){
    vector< pair< int,pair<int,int> > > ret;
    REP(i,p.size()){
	ret.PB(pair<int,pair<int,int> >(p[i].dir,pair<int,int>(p[i].x,p[i].y)));
    }
    sort(RALL(ret));
    REP(i,p.size()){
	p[i].dir = ret[i].fr;
	p[i].x = ret[i].sc.fr;
	p[i].y = ret[i].sc.sc;
    }
}

int main(){
    while(true){
	int w,h;
	cin >> w >> h;
	if(w == 0 && h == 0) break;
	vector< vector<char> > board(h,vector<char>(w));
	vector< struct person > people;
	REP(i,h)REP(j,w){
	    cin >> board[i][j];
	    bool flg = false;
	    REP(k,4){
		if(dir[k] == board[i][j]) flg = true;
	    }
	    if(flg){
		struct person p;
		p.x = j;
		p.y = i;
		REP(k,4){
		    if(board[i][j] == dir[k]){
			p.dir = k;
			break;
		    }
		}
		people.PB(p);
	    }
	}
	int ans = -1;
	REP(a,181){
	    if(people.size() == 0){
		ans = a;
		break;
	    }
	    vector< vector<char> > board1 = board;
	    REP(i,people.size()){
		int x = people[i].x;
		int y = people[i].y;
		board1[y][x] = '.';
		/*
		cout << "board1[y][x] = "<< board1[y][x] << endl;
		REP(i,h){
		    REP(j,w){
			cout << board1[i][j];
		    }
		    cout << endl;
		}
		cout << "board[y][x] = "<< board[y][x] << endl;
		REP(i,h){
		    REP(j,w){
			cout << board[i][j];
		    }
		    cout << endl;
		}
		*/
	    }
	    REP(i,h)REP(j,w){
		if(board1[i][j] == 'X') board1[i][j] = 'x';
	    }
	    RREP(i,people.size()){
		REP(k,4){
		    int nk = (ddir[k]+people[i].dir)%4;
		    int nx = people[i].x + dx[nk];
		    int ny = people[i].y + dy[nk];
		    if(nx<0||w<=nx||ny<0||h<=ny) continue;
		    if(board[ny][nx] != '#'){
			/*
			cout << "===BEGIN===" << endl;
			printf("people.x = %d , people.y = %d\n",people[i].x,people[i].y);
			printf("board[%d][%d] = %c\n",ny,nx,board[ny][nx]);
			printf("nx = %d ny = %d\n",nx,ny);
			printf("dir = %d\n",people[i].dir);
			printf("nk = %d dx = %d dy = %d\n",nk,dx[nk],dy[nk]);
			cout << "===END===" << endl;
			*/
			bool leftflg = false;
			REP(j,4){
			    if(board[ny][nx] == dir[j]) leftflg =true;
			}
			if(leftflg) {
			    continue;
			}
			people[i].dir = nk;
			break;
		    }
		}
	    }
	    //sort(RALL(people));
	    Sort(people);
	    RREP(i,people.size()){
		int nk = people[i].dir;
		int nx = people[i].x + dx[nk];
		int ny = people[i].y + dy[nk];
		//printf("nx = %d ny = %d\n",nx,ny);
		if(board1[ny][nx] != '.'){
		    if(board1[ny][nx] == 'x'){
			board1[ny][nx] = 'X';
			people.erase(people.begin()+i);
			continue;
		    }
		    nx = people[i].x;
		    ny = people[i].y;
		}
		
		people[i].x = nx;
		people[i].y = ny;
		board1[ny][nx] = dir[people[i].dir];
	    }
	    board = board1;
	    /*
	    cout << "board1==== "<<endl;
	    REP(i,h){
		REP(j,w){
		    cout << board[i][j];
		}
		cout << endl;
	    }
	    */
	}
	if(a >= 0) cout << a << endl;
	else cout << "NA" << endl;
    }
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:159:12: error: 'a' was not declared in this scope
  159 |         if(a >= 0) cout << a << endl;
      |            ^