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<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<functional>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;

#define mind(a,b) (a>b?b:a)
#define maxd(a,b) (a>b?a:b)
#define absd(x) (x<0?-(x):x)
#define pow2(x) ((x)*(x))
#define rep(i,n) for(int i=0; i<n; ++i)
#define repr(i,n) for(int i=n-1; i>=0; --i)
#define repl(i,s,n) for(int i=s; i<=n; ++i)
#define replr(i,s,n) for(int i=n; i>=s; --i)
#define repf(i,s,n,j) for(int i=s; i<=n; i+=j)
#define repe(e,obj) for(auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X,Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i,n,val) for(int i=0; i<n; ++i) cout << val << " "; cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 10000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
//typedef pair<ll, ll> P;
typedef pair<P, int> PI;
typedef pair<int, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P> > pvqueue;

#define S 23

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

int main() {
  int h, w;
  char s[S][S];
  int dist[S][S];
  int sx, sy, gx, gy;
  string pat;
  while(cin >> h >> w && h+w) {
    rep(i, h) rep(j, w) {
      dist[i][j] = INF;
      char c;
      cin >> c;
      s[i][j] = c;
      if(c != '#') {
        s[i][j] = '.';
        if(c == 'A') {
          sx = j; sy = i;
        } else if(c == 'B') {
          gx = j; gy = i;
        }
      }
    }
    cin >> pat;

    queue<P> que;
    que.push(P(sx, sy));
    dist[sy][sx] = 0;
    while(!que.empty()) {
      P e = que.front(); que.pop();
      int x = e.first, y = e.second;
      rep(k, 4) {
        int nx = x + dx[k], ny = y + dy[k];
        if(!(0 <= nx && nx < w && 0 <= ny && ny < h) || dist[ny][nx] < INF || s[ny][nx] == '#') {
          continue;
        }
        dist[ny][nx] = dist[y][x] + 1;
        que.push(P(nx, ny));
      }
    }

    int ans = INF;
    repl(i, 1, w*h*pat.length()) {
      int com = pat[(i-1)%pat.length()] - '0';
      int cy = 1 - (com - 1)/3, cx = ((com - 1)%3) - 1;
      if(0 <= gx + cx && gx + cx < w && 0 <= gy + cy && gy + cy < h) {
        gx += cx; gy += cy;
      }
      int t = dist[gy][gx];
      if(t <= i) {
        ans = i;
        cout << i SP gy SP gx << endl;
        break;
      }
    }
    if(ans == INF) {
      cout << "impossible" << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <memory.h>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <cmath>
using namespace std;

#define rep(i, n) for(int i = 0; i< n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> state;
const int INF = 1 << 29;
const double EPS = 1e-10;

int H, W;
int memo[12][500][500];
int dx[] = {1, 0, -1, 0, 0};
int dy[] = {0, 1, 0, -1, 0};
char field[22][22];

int h(int x, int y){
  return y * W + x;
}

int main(){
  int sx, sy, gx, gy;
  string order;
  while(cin >> H >> W && (H || W)){
    rep(i, H) rep(j, W){
      cin >> field[i][j];
      if(field[i][j] == 'A'){
	field[i][j] = '.';
	sy = i;
	sx = j;
      }
      if(field[i][j] == 'B'){
	field[i][j] = '.';
	gy = i;
	gx = j;
      }
    }
    cin >> order;
    bool f = false;
    queue<state> que;
    memset(memo, -1, sizeof(memo));
    que.push(state(0, P(h(sx, sy), h(gx, gy))));
    memo[0][h(sx, sy)][h(gx, gy)] = 0;
    while(!que.empty()){
      int ngx, ngy;
      state st = que.front();que.pop();
      int turn = st.first;
      sx = st.second.first % W;
      sy = st.second.first / W;
      gx = st.second.second % W;
      gy = st.second.second / W;
      //      cout << turn << " " << sx << " "<< sy << endl;
      if(sx == gx && sy == gy){
	cout << memo[turn][h(sx,sy)][h(gx,gy)] << " " << sy << " " << gx << endl;
	f = true;
	break;
      }
 
      ngx = gx, ngy = gy;
      if(order[turn] == '8') ngx = gx, ngy = gy - 1;
      if(order[turn] == '6') ngx = gx + 1, ngy = gy;
      if(order[turn] == '4') ngx = gx - 1, ngy = gy;
      if(order[turn] == '2') ngx = gx, ngy = gy + 1;
      if(0 > ngx || ngx >= W || 0 > ngy || ngy >= H){
	ngx = gx;
	ngy = gy;
      }
      rep(i, 5){
	int nturn = (turn + 1) % (int)order.size();
	int nsy = sy + dy[i];
	int nsx = sx + dx[i];
	if(0<=nsx && nsx<W && 0<=nsy && nsy<H && field[nsy][nsx] == '.'){
	  if(memo[nturn][h(nsx,nsy)][h(ngx, ngy)] == -1){
	    memo[nturn][h(nsx,nsy)][h(ngx,ngy)]=memo[turn][h(sx,sy)][h(gx,gy)]+1;
	    que.push(state(nturn, P(h(nsx, nsy), h(ngx, ngy))));
	  }
	}
      } 
    } 
    if(!f) cout << "impossible" << endl;
  }
  return 0;
}