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 <algorithm>
#include <iostream>
#include <queue>
#define m make_pair
#define INF (1e9)
#define N 20
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> P1;
void bfs();
int h,w,sx,sy,gy,gx,p,d[N][N],anst,ansy,ansx;
string str[N],pat;

int main(){
  while(1){
    cin>>h>>w;
    if(!h&&!w) break;
    for(int i=0;i<h;i++) cin>>str[i];
    cin>>pat;
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++){
	if(str[i][j]=='A') sx=j,sy=i;
	if(str[i][j]=='B') gx=j,gy=i;
      }
    bfs();
    int len=pat.size(),t=0,eflag=0;
    p=0;
    while(1){
      int cy=gy,cx=gx;
      if(pat[p]=='8') gy--;
      if(pat[p]=='6') gx++;
      if(pat[p]=='4') gx--;
      if(pat[p]=='2') gy++;
      p++;
      t++;
      if(p==len) p=0;
      if(gy<0||h<=gy||gx<0||w<=gx) gy=cy,gx=cx;
      if(d[gy][gx]!=INF&&d[gy][gx]<=t){
	anst=max(d[gy][gx],t);
	ansy=gy;
	ansx=gx;
	break;
      }
      if(t==100000){
	eflag=1;
	break;
      }
    }
    if(!eflag) cout<<anst<<' '<<ansy<<' '<<ansx<<endl;
    else cout<<"impossible"<<endl;
  }
  return 0;
}

void bfs(){
  int dy[4]={-1,0,1,0};
  int dx[4]={0,1,0,-1};
  queue<P1> q;
  for(int i=0;i<N;i++)
    for(int j=0;j<N;j++) d[i][j]=INF;
  d[sy][sx]=0;
  q.push(m(0,m(sy,sx)));
  while(!q.empty()){
    P1 t=q.front(); q.pop();
    int c=t.first,uy=t.second.first,ux=t.second.second;
    for(int i=0;i<4;i++){
      int ny=uy+dy[i],nx=ux+dx[i];
      if(ny<0||h<=ny||nx<0||w<=nx) continue;
      if(str[ny][nx]=='#') continue;
      if(d[ny][nx]>c+1){
	d[ny][nx]=c+1;
	q.push(m(d[ny][nx],m(ny,nx)));
      }
    }
  }
}

C++: #include<iostream>
#include<map>
#include<set>
#include<string>
#include<algorithm>
using namespace std;
int t[20][20];
const int inf = 1000;
void solve(int sum, int i, int j, int h, int w, char t2[20][20]) {
	if (i + 1 < h&&t2[i + 1][j] == '.'&&t[i+1][j]>sum+1) {
		t[i + 1][j] = sum + 1;
		solve(sum + 1, i + 1, j, h, w, t2);
	}
	if (i - 1 >= 0 && t2[i - 1][j] == '.'&&t[i-1][j]>sum+1) {
		t[i - 1][j] = sum + 1;
		solve(sum + 1, i - 1, j, h, w, t2);
	}
	if (j + 1 < w&&t2[i][j + 1] == '.'&&t[i][j+1]>sum+1) {
		t[i][j + 1] = sum + 1;
		solve(sum + 1, i, j + 1, h, w, t2);
	}
	if (j - 1 >= 0 && t2[i][j - 1] == '.'&&t[i][j-1]>sum+1) {
		t[i][j - 1] = sum + 1;
		solve(sum + 1, i, j - 1, h, w, t2);
	}
}
int main() {
	int h, w;
	char t2[20][20];
	int t3[20][20][10];
	while (cin >> h >> w&&h != 0 && w != 0) {
		string u;
		int p[2];
		int g[2];
		for (int i = 0; i < h; i++) {
			cin >> u;
			for (int j = 0; j < w; j++) {
				t2[i][j] = u[j];
				if (u[j] == 'A') {
					p[0] = i;
					p[1] = j;
				}
				if (u[j] == 'B') {
					g[0] = i;
					g[1] = j;
				}
				t[i][j] = inf;
				for (int z = 0; z < 10; z++) t3[i][j][z] = 0;
			}
		}
		string a;
		cin >> a;
		int b[10][2];
		for (int i = 0; i < a.length(); i++) {
			switch (a[i])
			{
			case '5':
				b[i][0] = 0;
				b[i][1] = 0;
				break;
			case '8':
				b[i][0] = -1;
				b[i][1] = 0;
				break;
			case '6':
				b[i][0] = 0;
				b[i][1] = 1;
				break;
			case '4':
				b[i][0] = 0;
				b[i][1] = -1;
				break;
			case '2':
				b[i][0] = 1;
				b[i][1] = 0;
			default:
				break;
			}
		}
		t[p[0]][p[1]] = 0;
		t2[g[0]][g[1]] = '.';
		int sum2 = 0;
		bool l = false;
		solve(0, p[0], p[1], h, w, t2);
		int k = 0, x = g[0], y = g[1], sum = 0;
		while (true) {
			x += b[k][0];
			y += b[k][1];
			k++;
			sum++;
			if (k >= a.length()) k = 0;
			x = max(0, min(x, h - 1));
			y = max(0, min(y, w - 1));
			if (t[x][y] <= sum) {
				cout << sum << " " << x << " " << y << endl;
				break;
			}
			if (t3[x][y][k] == 1&&!l) {
				sum2 = 0;
				l = true;
			}
			if (t3[x][y][k] == 2) {
				if (sum2 == 0) {
					cout << "impossible" << endl;
					break;
				}
			}
			if (l&&t[x][y]!=inf) {
				sum2++;
			}
			t3[x][y][k]++;
		}
	}
}