### constant

MAX_INT = 1 << 30

HDXYS = {
  "5" => [0, 0],
  "8" => [0, -1], "6" => [1, 0], "4" => [-1, 0], "2" => [0, 1]
}
DXYS = [[0, 0], [0, -1], [1, 0], [-1, 0], [0, 1]]

### main

loop do
  h, w = gets.split.map(&:to_i)
  break if h == 0 && w == 0

  hw = h * w
  mat = h.times.map{w.times.map{true}}
  gst = hst = nil

  for y in (0...h)
    line = gets.strip
    for x in (0...w)
      case line[x]
      when '#'
        mat[y][x] = false
      when 'A'
        gst = [x, y]
      when 'B'
        hst = [x, y]
      end
    end
  end
  #p mat
  #p [gst, hst]

  hpat = gets.strip
  nhpat = hpat.length
  #p [nhpat, hpat]

  dists = nhpat.times.map{hw.times.map{hw.times.map{MAX_INT}}}
  dists[0][gst[0] + gst[1] * w][hst[0] + hst[1] * w] = 0

  q = [[0, *gst, *hst]]
  #p q

  min_t = []

  while ! q.empty?
    ut, ugx, ugy, uhx, uhy = q.shift
    if ugx == uhx && ugy == uhy
      min_t = [dists[ut][ugx + ugy * w][uhx + uhy * w], ugy, ugx]
      break
    end

    du = dists[ut][ugx + ugy * w][uhx + uhy * w]

    hdx, hdy = HDXYS[hpat[ut]]
    vhx = uhx + hdx
    vhy = uhy + hdy
    if vhx < 0 || vhx >= w || vhy < 0 || vhy >= h
      vhx = uhx
      vhy = uhy
    end
    vhid = vhx + vhy * w

    vt = (ut + 1) % nhpat

    for dxy in DXYS
      vgx = ugx + dxy[0]
      vgy = ugy + dxy[1]

      if vgx >= 0 && vgx < w && vgy >= 0 && vgy < h && mat[vgy][vgx]
        vgid = vgx + vgy * w
        dv = dists[vt][vgid][vhid]
        if dv > du + 1
          dists[vt][vgid][vhid] = du + 1
          if dv >= MAX_INT
            q << [vt, vgx, vgy, vhx, vhy]
          end
        end
      end
    end
  end

  if min_t.empty?
    puts 'impossible'
  else
    puts min_t.join(' ')
  end
end