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 <vector>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;

const int INF = 1e+8;
typedef pair<int,int> P;
typedef pair<P,P> PP;
typedef pair<PP,int> State;

// 入力
int w, h;
string s[20], p;

// d[ay][ax][by][bx][i] := (ax,ay),(bx,by),i分目のときの手数
// 5次元配列とかやばすぎ.
int d[20][20][20][20][10];

int dx[256] = {0,0,-1,1};
int dy[256] = {-1,1,0,0};

void debug(int ax, int ay, int bx, int by, int t){
	cout << "[debug]" << " t:" << t << endl;
	for(int y=0 ; y < h ; y++ ){
		for(int x=0 ; x < w ; x++ ){
			if( x == ax && y == ay ){
				cout << "A";
			}else if( x == bx && y == by ){
				cout << "B";
			}else{
				cout << s[y][x];
			}
		}
		cout << endl;
	}
	cout << endl;
}

// 初期化 
void ini(){
	for(int ay=0 ; ay < h ; ay++ ){
		for(int ax=0 ; ax < w ; ax++ ){
			for(int by=0 ; by < h ; by++ ){
				for(int bx=0 ; bx < w ; bx++ ){
					for(int i=0 ; i < p.size() ; i++ ){
						d[ay][ax][by][bx][i] = INF;
					}
				}
			}
		}
	}
}

// 幅優先探索.
pair<int,P> bfs(int ax, int ay, int bx, int by){
	queue<State> q;
	PP pp(P(ax,ay),P(bx,by));
	q.push( State( pp , 0 ) );
	d[ay][ax][by][bx][0] = 0;
	ini();
	
	while( !q.empty() ){
		int ax = q.front().first.first.first;
		int ay = q.front().first.first.second;
		int bx = q.front().first.second.first;
		int by = q.front().first.second.second;
		int t  = q.front().second;
		q.pop();
		//debug(ax,ay,bx,by,t);
		
		int bx_ = bx + dx[p[t % p.size()]];
		int by_ = by + dy[p[t % p.size()]];
		if( bx_ < 0 || w <= bx_ ) bx_ = bx;
		if( by_ < 0 || h <= by_ ) by_ = by;
		P b(bx_,by_);
		for(int i=0 ; i < 4 ; i++ ){
			int ax_ = ax + dx[i];
			int ay_ = ay + dy[i];
			if( ax_ < 0 || w <= ax_ ) ax_ = ax;
			if( ay_ < 0 || h <= ay_ ) ay_ = ay;
			if( s[ay_][ax_] == '#' ){
				ax_ = ax; ay_ = ay;
			}
			P a(ax_,ay_);
			
			if( a == b ){ // ゴールの時
				return pair<int,P>(t+1,a);
			}else if( d[ay_][ax_][by_][bx_][(t+1) % p.size()] == INF ){
				d[ay_][ax_][by_][bx_][(t+1) % p.size()] = t+1;
				PP pp(P(ax_,ay_),P(bx_,by_));
				q.push( State( pp , t+1 ) );
			}
		}
	}
	return pair<int,P>(-1,P(0,0));
}

int main(){
	dx['6'] = 1; dx['4'] = -1; dy['8'] = -1; dy['2'] = 1;
	
	while( cin >> h >> w , h || w ){
		for(int y=0 ; y < h ; y++ ){
			cin >> s[y];
		}
		cin >> p;
		
		int ax, ay, bx, by;
		for(int y=0 ; y < h ; y++ ){
			for(int x=0 ; x < w ; x++ ){
				if( s[y][x] == 'A' ){
					ax = x; ay = y; s[y][x] = '.';
				}
				if( s[y][x] == 'B' ){
					bx = x; by = y; s[y][x] = '.';
				}
			}
		}
		
		pair<int,P> ans = bfs(ax, ay, bx, by);
		if( ans.first == -1 ){
			cout << "impossible" << endl;
		}else{
			cout << ans.first << " ";
			cout << ans.second.second << " ";
			cout << ans.second.first << endl;
		}
	}
}