Direction = ["E", "N", "W", "S"]
Blank = [".", "X"]
hash = Hash[*Direction.zip(0...4).flatten]
Next = [
  [0, 1],
  [-1, 0],
  [0, -1],
  [1, 0],
]

class Person
  attr_reader :y, :x, :d
  attr_accessor :movable
  def initialize(y, x, d)
    @y, @x, @d = y, x, d
    @movable = nil
  end
  def turn(d)
    case d
      when :Right
        @d = (4 + @d - 1) % 4
      when :Left
        @d = (@d + 1) % 4
    end
  end
  def front
    dy, dx = Next[@d]
    [@y + dy, @x + dx]
  end
  def move
    @y, @x = front
  end
end

until (w, h = gets.split.map(&:to_i)).all?(&:zero?)
  map = h.times.map{gets.chomp.chars}
  person = Array.new

  h.times do |i|
    w.times do |j|
      person << Person.new(i, j, hash[map[i][j]]) if Direction.include?(map[i][j])
    end
  end

  time = 0
  until person.empty? || time > 180
    time += 1
    person.sort!{|a, b|
      result = 0
      result = -1 if a.x > b.y
      result = 1 if a.x < b.y && result == 0
      result = -1 if a.x < b.y && result == 0
      result = 1 if a.x > b.y && result == 0
      0
    }
    out = Array.new
    # 向き
    person.each do |p|
      p.turn(:Right)
      4.times do
        y, x = p.front
        break if Blank.include?(map[y][x])
        p.turn(:Left)
      end
      y, x = p.front
      p.turn(:Left) unless Blank.include?(map[y][x])
      p.movable = Blank.include?(map[y][x])
    end
    # 移動
    used = Array.new
    person.each do |p|
      y, x = p.front
      case map[y][x]
        when "X"
          next if used.include?([y, x])
          used << [y, x]
          out << p
          map[p.y][p.x] = "."
        when "."
          next unless p.movable
          map[y][x], map[p.y][p.x] = map[p.y][p.x], map[y][x]
          p.move
      end
    end
    # 削除
    person.delete(out.shift) until out.empty?
  end
  puts (time > 180 ? "NA" : time)
end