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 <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a,c) memset(a,c,sizeof(a))

#define DEBUG(x) cout<<"#x"<<": "<<x<<endl

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;

const ll INF = INT_MAX/3;
const ll MOD = 1000000007;
const double EPS = 1e-14;
const int dx[] = {1,0,-1,0} , dy[] = {0,1,0,-1};

int h,w;
int board[21][21];
vector<string> ss;

void moveG(int &x,int &y,char p){
    int tx,ty;
    tx = x;
    ty = y;
    if(p == '5'){
	return;
    }else if(p == '8'){
	if(y > 0)
	    y--;
    }else if(p == '6'){
	if(x < w-1)
	    x++;
    }else if(p == '4'){
	if(x > 0)
	    x--;
    }else if(p == '2'){
	if(y < h-1)
	    y++;
    }

}
int main(){
    while(true){
	cin >> h >> w;
	if(h == 0 && w == 0) break;
	string pattern;
	ss = vector<string>(h);
	REP(i,h) cin >> ss[i];
	cin >> pattern;

	int sx,sy,gx,gy;
	int ans = 0;
	REP(i,h)REP(j,w){
	    board[i][j] = INF;
	    if(ss[i][j] == 'A'){
		sx = j;
		sy = i;
	    }else if(ss[i][j] == 'B'){
		gx = j;
		gy = i;
	    }
	}

	bool flg = false;
	int p = 0;
	int t = 0;
	board[sy][sx] = 0;
	queue<int> qx;
	queue<int> qy;
	qx.push(sx);
	qy.push(sy);
	while(!qx.empty()){
	    int x = qx.front();
	    int y = qy.front();
	    qx.pop();
	    qy.pop();
	    int v = board[y][x]+1;
	    REP(i,4){
		int nx = x + dx[i];
		int ny = y + dy[i];
		if(nx < 0 || w <= nx || ny < 0 || h <= ny || ss[ny][nx] == '#') continue;
		if(board[ny][nx] > v){
		    board[ny][nx] = v;
		    qx.push(nx);
		    qy.push(ny);
		}
	    }
	}
	bool u[31][31];
	REP(i,31)REP(j,31) u[i][j] = false;
	while(t < 1000){
	    if(board[gy][gx] <= t ){
		printf("%d %d %d\n",t,gy,gx);
		flg = true;
		break;
	    }
	    moveG(gx,gy,pattern[p]);
	    p = (p+1) % pattern.size();
	    t++;
	}
	if(!flg) cout << "impossible" << endl;
    }

    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <complex>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <iterator>
#include <tuple>

using namespace std;

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define iter(c) __typeof__((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INFTY=1<<29;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

int main()
{
	for(int h,w;cin>>h>>w,h|w;){
		vs grid(h);
		rep(i,h) cin>>grid[i];
		string p; cin>>p;
		
		vvi t(h,vi(w,INFTY));
		queue<tuple<int,int,int>> q;
		rep(i,h) rep(j,w) if(grid[i][j]=='A')
			q.emplace(i,j,0);
		while(!q.empty()){
			auto cur=q.front(); q.pop();
			int ci=get<0>(cur),cj=get<1>(cur),ct=get<2>(cur);
			if(ci<0 || h<=ci || cj<0 || w<=cj || grid[ci][cj]=='#' || t[ci][cj]!=INFTY)
				continue;
			t[ci][cj]=ct;
			rep(i,4) q.emplace(ci+"\xff\x1\0\0"[i],cj+"\0\0\xff\x1"[i],ct+1);
		}
		
		vvi mask(h,vi(w,INFTY));
		int ci=-1,cj=-1;
		rep(i,h) rep(j,w) if(grid[i][j]=='B')
			ci=i,cj=j;
		int res=INFTY;
		pii pos;
		for(int ct=0,i=0;;ct++,i=(i+1)%p.size()){
			if(res>max(ct,t[ci][cj])){
				res=max(ct,t[ci][cj]);
				pos=mp(ci,cj);
			}
			if(mask[ci][cj]&1<<i)
				break;
			mask[ci][cj]|=1<<i;
			int ni=ci,nj=cj;
			if(p[i]=='8') ni--;
			if(p[i]=='2') ni++;
			if(p[i]=='4') nj--;
			if(p[i]=='6') nj++;
			if(0<=ni && ni<h && 0<=nj && nj<w)
				ci=ni,cj=nj;
		}
		
		if(res==INFTY)
			cout<<"impossible"<<endl;
		else
			cout<<res<<' '<<pos.first<<' '<<pos.second<<endl;
	}
	
	return 0;
}