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++: //#define LOCAL

#include <fstream>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <map>

#define rep(i,n) for(int i=0; i<n; i++)

using namespace std;

int H, W;
char grid[25][25];
string pat;
int grid2[25][25];
int ay, ax;
int by, bx;

void bfs()
{
    const int INF = 100000000;
    int dy[4] = {0, -1, 0, 1};
    int dx[4] = {-1, 0, 1, 0};

    typedef pair<int, int> P;

    queue<P> que;
    rep(i,H) rep(j,W) {
        grid2[i][j] = INF;
    }

    que.push(P(ay, ax));
    grid2[ay][ax] = 0;

    while (que.size()) {
        P p = que.front(); que.pop();
        rep(i,4) {
            int ny = p.first + dy[i];
            int nx = p.second + dx[i];

            if (0 <= ny && ny < H && 0 <= nx && nx < W && grid[ny][nx] == '.' && grid2[ny][nx] == INF) {
                que.push(P(ny, nx));
                grid2[ny][nx] = grid2[p.first][p.second] + 1;
            }
        }
    }
}

int main()
{
#ifdef LOCAL
    ifstream in("input.txt");
    cin.rdbuf(in.rdbuf());
#endif

    while (true) {
        cin >> H >> W;
        if (H == 0 && W == 0) break;

        rep(i,H) rep(j,W) {
            cin >> grid[i][j];
            if (grid[i][j] == 'A') {
                ay = i;
                ax = j;
                grid[i][j] = '.';
            } else if (grid[i][j] == 'B') {
                by = i;
                bx = j;
                grid[i][j] = '.';
            }
        }
        cin >> pat;

        /*
        int sy = 0, sx = 0;
        rep(i,pat.length()) {
            if (pat[i] == '5') {
                sy += 0;
                sx += 0;
            } else if(pat[i] == '4') {
                sy += 0;
                sx -= 1;
            } else if (pat[i] == '8') {
                sy -= 1;
                sx += 0;
            } else if (pat[i] == '6') {
                sy += 0;
                sx += 1;
            } else if (pat[i] == '2') {
                sy += 1;
                sx += 0;
            }
        }
        bool try_only_one = false;
        if (sy == 0 && sx == 0) try_only_one = true;
        */

        bfs();

        /*
        rep(i,H) {
            rep(j,W) {
                printf("% 3d ", grid2[i][j]);
            }
            cout << endl;
        }
        */

        bool possible = false;
        int ans;
        int ansy, ansx;
        int cnt = 0;
        rep(i,25) {
            rep(j,pat.length()) {
                cnt++;
                if (pat[j] == '5') {
                    by += 0;
                    bx += 0;
                } else if(pat[j] == '4') {
                    by += 0;
                    if (bx >= 1) bx -= 1;
                } else if (pat[j] == '8') {
                    if (by >= 1) by -= 1;
                    bx += 0;
                } else if (pat[j] == '6') {
                    by += 0;
                    if (bx < W - 1) bx += 1;
                } else if (pat[j] == '2') {
                    if (by < H - 1) by += 1;
                    bx += 0;
                }
                if (grid2[by][bx] <= cnt && !possible) {
                    possible = true;
                    ans = cnt;
                    ansy = by;
                    ansx = bx;
                }
            }
        }

        if (possible) {
            cout << ans << " " << ansy << " " << ansx << endl;
        } else {
            cout << "impossible" << endl;
        }
    }
}

C++: #include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
using namespace std;
int dx[]={0,1,0,-1,0};
int dy[]={1,0,-1,0,0};
char m[30][30];
char str[30];
int bfs[30][30];
int main(){
	int a,b;
	while(scanf("%d%d",&a,&b),a){
		for(int i=0;i<a;i++)scanf("%s",m[i]);
		scanf("%s",str);
		for(int i=0;i<a;i++)for(int j=0;j<b;j++)bfs[i][j]=-1;
		queue<pair<int,int> > Q;
		for(int i=0;i<a;i++)for(int j=0;j<b;j++)if(m[i][j]=='A'){
			Q.push(make_pair(i,j));
			bfs[i][j]=0;
		}
		while(Q.size()){
			int row=Q.front().first;
			int col=Q.front().second;
			Q.pop();
			for(int i=0;i<4;i++){
				if(0<=row+dx[i]&&row+dx[i]<a&&0<=col+dy[i]&&col+dy[i]<b&&m[row+dx[i]][col+dy[i]]!='#'&&!~bfs[row+dx[i]][col+dy[i]]){
					bfs[row+dx[i]][col+dy[i]]=bfs[row][col]+1;
					Q.push(make_pair(row+dx[i],col+dy[i]));
				}
			}
		}
		int row,col;
		for(int i=0;i<a;i++)for(int j=0;j<b;j++)if(m[i][j]=='B'){
			row=i;col=j;
		}
		int len=strlen(str);
		bool ok=false;
		for(int i=0;i<500;i++){
			if(~bfs[row][col]&&bfs[row][col]<=i){
				printf("%d %d %d\n",i,row,col);ok=true;break;
			}
			if(str[i%len]=='2')row=min(row+1,a-1);
			if(str[i%len]=='4')col=max(col-1,0);
			if(str[i%len]=='6')col=min(col+1,b-1);
			if(str[i%len]=='8')row=max(row-1,0);
		}
		if(!ok)printf("impossible\n");
	}
}