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<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] == '.') {
		t2[i + 1][j] = '#';
		t[i + 1][j] = sum + 1;
		solve(sum + 1, i + 1, j, h, w, t2);
	}
	if (i - 1 >= 0 && t2[i - 1][j] == '.') {
		t2[i - 1][j] = '#';
		t[i - 1][j] = sum + 1;
		solve(sum + 1, i - 1, j, h, w, t2);
	}
	if (j + 1 < w&&t2[i][j + 1] == '.') {
		t2[i][j + 1] = '#';
		t[i][j + 1] = sum + 1;
		solve(sum + 1, i, j + 1, h, w, t2);
	}
	if (j - 1 >= 0 && t2[i][j - 1] == '.') {
		t2[i][j - 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]++;
		}
	}
}

C++: #define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
using namespace std;

long INF = 1000000;

int main() {

    int h, w;
    while(cin >> h >> w && (h || w)) {
        queue<int> x;
        queue<int> y;
        char town[h][w];
        long time[h][w];
        int gx, gy;
        string str;
        getline(cin, str);
        for(int i = 0; i < h; i++) {
            getline(cin, str);
            for(int j = 0; j < w; j++) {
                time[i][j] = INF;
                town[i][j] = str[j];
                if(str[j] == 'A') {
                    x.push(i);
                    y.push(j);
                    town[i][j] = '.';
                    time[i][j] = 0;
                }
                if(str[j] == 'B') {
                    gx = i;
                    gy = j;
                    town[i][j] = '.';
                }
            }
        }

        while(!x.empty()) {
            int i = x.front();
            int j = y.front();
            x.pop();
            y.pop();
            if ( i > 0 && town[i-1][j] != '#' && time[i-1][j] == INF ) {
                time[i-1][j] = time[i][j] + 1;
                x.push(i-1);
                y.push(j);
            }
            if ( j > 0 && town[i][j-1] != '#' && time[i][j-1] == INF ) {
                time[i][j-1] = time[i][j] + 1;
                x.push(i);
                y.push(j-1);
            }
            if ( i < h - 1 && town[i+1][j] != '#' && time[i+1][j] == INF ) {
                time[i+1][j] = time[i][j] + 1;
                x.push(i+1);
                y.push(j);
            }
            if ( j < w - 1 && town[i][j+1] != '#' && time[i][j+1] == INF ) {
                time[i][j+1] = time[i][j] + 1;
                x.push(i);
                y.push(j+1);
            }
        }

        getline(cin, str);

        long t = 1;
        int lenpat = str.length();
        while(t < lenpat * 1000) {
            switch(str[(t - 1) % lenpat]) {
                case '2':
                    if (gx < h - 1) gx += 1;
                    break;
                case '4':
                    if (gy > 0) gy -= 1;
                    break;
                case '6':
                    if (gy < w - 1) gy += 1;
                    break;
                case '8':
                    if (gx > 0) gx -= 1;
                    break;
            }
            if (time[gx][gy] <= t) {
                cout << t << " " << gx << " " << gy << endl; 
                break;
            }
            t += 1;
        }
        if (t == lenpat * 1000) {
            cout << "impossible" << endl;
        }

    }
     
    return 0;
}