Bearing = ["E", "N", "W", "S"]
Direction = [*0...4]
Blank = [".", "X"]
Next = [
  [0, 1],
  [-1, 0],
  [0, -1],
  [1, 0],
]

until (w, h = gets.split.map(&:to_i)).all?(&:zero?)
  map = h.times.map{gets.chomp.chars}
  h.times do |i|
    w.times do |j|
      map[i][j] = Bearing.index(map[i][j]) if Bearing.include?(map[i][j])
    end
  end

  time = 0
  while map.flatten.any?{|c| Direction.include?(c)} && time <= 180
    time += 1

    # turn
    h.times do |i|
      w.times do |j|
        next unless Direction.include?(map[i][j])
        map[i][j] = (map[i][j] - 1 + 4) % 4
        5.times do
          dy, dx = Next[map[i][j]]
          break if Blank.include?(map[i + dy][j + dx])
          map[i][j] = (map[i][j] + 1) % 4
        end
      end
    end

    # move
    used = Array.new
    h.times do |i|
      w.times do |j|
        next unless Blank.include?(map[i][j])
        4.times do |d|
          dy, dx = Next[d]
          y, x = i + dy, j + dx
          next unless Direction.include?(map[y][x])
          next unless (map[y][x] - d + 4) % 4 == 2
          next if used.include?([y, x])
          used << [i, j]
          map[y][x], map[i][j] = map[i][j], map[y][x] if map[i][j] == "."
          map[y][x] = "." if map[i][j] == "X"
          break
        end
      end
    end
  end

  puts (time > 180 ? "NA" : time)
end