#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);
      }
    }

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