
object Main extends App {

  import scala.io.StdIn._
  import scala.math._

  solve
  def solve: Unit ={
    val Array(h, w) = readLine.trim.split(' ').map(_.toInt)
    if (h == 0) return
    val state = Array.tabulate(h){_ ⇒ readLine.trim}
    val girl = state.indices.flatMap(h ⇒ state(h).indices.map{w ⇒ Coordinate(w, h)}).find{c ⇒ state(c.y)(c.x) == 'A'}.get
    val minTime = Array.tabulate(h){_ ⇒ Array.tabulate(w){_ ⇒ Int.MaxValue}}.also{arr ⇒ arr(girl.y)(girl.x) = 0}
    var current = List(girl)
    while (current.nonEmpty){
      var next = Nil: List[Coordinate]
      for {c ← current; n ← c.neighbor} {
        if ((0 until h).contains(n.y) && (0 until w).contains(n.x) && state(n.y)(n.x) != '#' && minTime(n.y)(n.x) > minTime(c.y)(c.x) + 1) {
          minTime(n.y)(n.x) = minTime(c.y)(c.x) + 1
          next ::= n
        }
      }
      current = next
    }
    val pattern = readLine.trim.map{_.asDigit}.toArray
    val count = Array.tabulate(h){_ ⇒ Array.tabulate(w){_ ⇒ 0}}
    var ghost = state.indices.flatMap(h ⇒ state(h).indices.map{w ⇒ Coordinate(w, h)}).find{c ⇒ state(c.y)(c.x) == 'B'}.get

    var time = 0
    var firstContact: Option[(Int, Int, Int)] = None
    while (count(ghost.y)(ghost.x) <= pattern.length && firstContact.isEmpty){
      count(ghost.y)(ghost.x) += 1
      if (minTime(ghost.y)(ghost.x) <= time){
        firstContact = Some(time, ghost.y, ghost.x)
      }
      ghost = move(ghost, pattern(time % pattern.length), h, w)
      time += 1
    }
    var additional = 0
    while (firstContact.isEmpty && additional <= h * w){
      if (minTime(ghost.y)(ghost.x) <= time + additional){
        firstContact = Some(time + additional, ghost.y, ghost.x)
      }
      ghost = move(ghost, pattern((time + additional) % pattern.length), h, w)
      additional += 1
    }
    println(
      firstContact match {
        case Some((t, y, x)) ⇒ s"$t $y $x"
        case None ⇒ "impossible"
      }
    )
    solve
  }
  def move(coordinate: Coordinate, direction: Int, h: Int, w: Int): Coordinate = {
    (direction match {
      case 8 ⇒ Coordinate(0, -1) + coordinate
      case 6 ⇒ Coordinate(1, 0) + coordinate
      case 4 ⇒ Coordinate(-1, 0) + coordinate
      case 2 ⇒ Coordinate(0, 1) + coordinate
      case _ ⇒ coordinate
    }).let{c ⇒
      if ((0 until h).contains(c.y) && (0 until w).contains(c.x)) c
      else coordinate
    }
  }
  case class Coordinate(x: Int, y: Int) {
    def +(that: Coordinate): Coordinate = Coordinate(x + that.x, y + that.y)
    def neighbor: List[Coordinate] = List(Coordinate(x + 1, y), Coordinate(x - 1, y), Coordinate(x, y + 1), Coordinate(x, y - 1))
    def nextPosition: List[Coordinate] = this::neighbor
  }
  implicit class Extension[T](val value :T){
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
    def let[R](func: T ⇒ R): R = func(value)
  }
}
