Go: package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	dy := [4]int{-1, 0, 0, 1}
	dx := [4]int{0, -1, 1, 0}
	for {
		h := getNextInt(scanner)
		w := getNextInt(scanner)
		if h == 0 {
			break
		}
		grid := makeGrid(h, w)
		a := [2]int{}
		b := [2]int{}
		for i := 0; i < h; i++ {
			s := getNextString(scanner)
			for j := 0; j < w; j++ {
				if s[j] == '#' {
					grid[i][j] = math.MaxInt32
					continue
				}
				if s[j] == 'A' {
					a[0] = i
					a[1] = j
					continue
				}
				if s[j] == 'B' {
					b[0] = i
					b[1] = j
				}
				grid[i][j] = math.MaxInt32 - 1
			}
		}
		q := make([][2]int, 0)
		q = append(q, a)
		for len(q) > 0 {
			p := q[0]
			q = q[1:]
			for i := 0; i < 4; i++ {
				yy := p[0] + dy[i]
				xx := p[1] + dx[i]
				if yy == a[0] && xx == a[1] {
					continue
				}
				if yy < 0 || yy >= h {
					continue
				}
				if xx < 0 || xx >= w {
					continue
				}
				if grid[yy][xx] == math.MaxInt32 {
					continue
				}
				if grid[yy][xx] <= grid[p[0]][p[1]]+1 {
					continue
				}
				grid[yy][xx] = grid[p[0]][p[1]] + 1
				q = append(q, [2]int{yy, xx})
			}
		}
		move := getNextString(scanner)
		i := 0
		m := len(move)
		for {
			if i > 400 {
				fmt.Fprintln(writer, "impossible")
				break
			}
			if grid[b[0]][b[1]] <= i {
				fmt.Fprintln(writer, fmt.Sprintf("%d %d %d", i, b[0], b[1]))
				break
			}
			switch move[i%m] {
			case '8':
				if b[0] > 0 {
					b[0]--
				}
			case '6':
				if b[1]+1 < w {
					b[1]++
				}
			case '4':
				if b[1] > 0 {
					b[1]--
				}
			case '2':
				if b[0]+1 < h {
					b[0]++
				}
			}
			i++
		}
	}
}
func makeGrid(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define EACH(t,i,c) for(t::iterator i=(c).begin(); i!=(c).end(); ++i)
const double EPS = 1e-10;
const double PI  = acos(-1.0);

struct node{
    int y,x,t,cost;
    node(int y,int x,int t,int cost):y(y),x(x),t(t),cost(cost){}
    bool operator<(const node& other)const{
        return cost>other.cost;
    }
};

const int MAX=INT_MAX/10;
typedef vector<vvi> vvvi;
int dx[]={-1,0,1,0,0},dy[]={0,-1,0,1,0};

int main(){
    int n,m;
    while(cin>>n>>m,n){
        vvi field(n,vi(m));
		pii girl,ghost;
        REP(i,n){
			string s;
			cin>>s;
			REP(j,s.size()){
				switch(s[j]){
				case 'A':
					girl=make_pair(i,j);
					break;
				case 'B':
					ghost=make_pair(i,j);
					break;
				case '.':
					field[i][j]=0;
					break;
				case '#':
					field[i][j]=1;
					break;
				default:
					assert(0);
				}
			}
        }
		string pattern;
		cin>>pattern;
		int len=pattern.size()*50;

		vector<pii> glocs;
		REP(i,len){
			glocs.push_back(ghost);
			switch(pattern[i%pattern.size()]){
			case '2':
				if(ghost.first<n-1)ghost.first++;break;
			case '4':
				if(ghost.second>0)ghost.second--;break;
			case '8':
				if(ghost.first>0)ghost.first--;break;
			case '6':
				if(ghost.second<m-1)ghost.second++;break;
			case '5':
				break;
			default:
				assert(0);
			}
		}
        
        priority_queue<node> q;
		q.push(node(girl.first,girl.second,0,0));
        vvvi cost(n,vvi(m,vi(len,MAX)));
		cost[girl.first][girl.second][0]=0;
        while(!q.empty()){
            node cnode=q.top();q.pop();
            if(cost[cnode.y][cnode.x][cnode.t]<cnode.cost){
                continue;
            }
			REP(i,5){
				int x=cnode.x+dx[i];
				int y=cnode.y+dy[i];
				if(x>=0&&y>=0&&x<m&&y<n&&field[y][x]==0){
					int newcost=cnode.cost+1;
					int t=cnode.t+1;
					if(t<len&&cost[y][x][t]>newcost){
						cost[y][x][t]=newcost;
						q.push(node(y,x,t,newcost));
					}
				}
            }
        }
		int ans=MAX;
		pii ansloc;
		REP(i,len){
			int c=cost[glocs[i].first][glocs[i].second][i];
			if(c<ans){
				ans=c;
				ansloc=glocs[i];
			}
		}
		if(ans==MAX){
			cout<<"impossible"<<endl;
		}else{
			cout<<ans<<" "<<ansloc.first<<" "<<ansloc.second<<endl;
		}
    }
}