### constant

MAX_T = 180

M_NG = 0
M_OK = 1
M_EXIT = 2

DIRCH = {'E' => 0, 'N' => 1, 'W' => 2, 'S' => 3}
DIRS = [[1, 0], [0, -1], [-1, 0], [0, 1]]

### main

while true
  w, h = gets.strip.split(' ').map{|s| s.to_i}
  break if w == 0 && h == 0

  maze = h.times.map{[]}
  prss = []

  for y in (0...h)
    hl =  gets.strip
    for x in (0...w)
      case hl[x]
      when '#'
        maze[y][x] = M_NG
      when '.'
        maze[y][x] = M_OK
      when 'X'
        maze[y][x] = M_EXIT
      when 'E', 'N', 'W', 'S'
        maze[y][x] = M_NG
        prss << [x, y, DIRCH[hl[x]]]
      end
    end
  end
  #p maze
  #p prss

  t = 1

  while t <= MAX_T && ! prss.empty?
    mvpos = {}

    ## 1. change direction
    for prs in prss
      for dd in [3, 0, 1, 2]
        d0 = (prs[2] + dd) % 4
        x0 = prs[0] + DIRS[d0][0]
        y0 = prs[1] + DIRS[d0][1]
        if maze[y0][x0] != M_NG
          prs[2] = d0
          d1 = (d0 + 2) % 4
          key = [x0, y0].join(',')
          if ! mvpos.key?(key) || mvpos[key][0] > d1
            mvpos[key] = [d1, x0, y0, prs]
            #p mvpos[key]
          end
          break
        end
      end
    end

    ## 2. move
    for mvs in mvpos.values
      d1, x0, y0, prs = mvs
      maze[prs[1]][prs[0]] = M_OK
      if maze[y0][x0] == M_EXIT
        prss.delete(prs)
      else
        prs[0] = x0
        prs[1] = y0
        maze[y0][x0] = M_NG
      end
    end

    break if prss.empty?

    t += 1
  end

  puts (t <= MAX_T ? t : 'NA')
end