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 <sstream>
#include <string>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;

#define SIZE 21

class Point {
public:
    int r, c;
    Point( int r, int c ) : r(r), c(c) {};
    Point() { r = 0, c = 0; };

    bool operator == ( const Point& right ) const
    {
        return r == right.r && c == right.c;
    }

    bool operator < ( const Point& right ) const
    {
        return r != right.r ? r < right.r : c < right.c;
    }
};

typedef pair <Point, Point> PPP;
typedef pair <int, PPP> PIP;

int h, w;
int m[SIZE][SIZE];

// for girl
bool check_girl( Point p )
{
    int r = p.r, c = p.c;
    if ( r >= 0 && r < h && c >= 0 && c < w ) {
        if ( m[r][c] == 0 ) {
            return true;
        }
    }
    return false;
}

// for ghost
bool check_ghost( Point p )
{
    int r = p.r, c = p.c;
    return r >= 0 && r < h && c >= 0 && c < w;
}

int main( void )
{
    string line;
    while ( getline( cin, line ) ) {
        // input
        istringstream is(line);
        is >> h >> w;
        if ( h == 0 && w == 0 ) break;
        
        for ( int i = 0; i < h; i++ )
        {
            getline( cin, line );
            for ( int j = 0; j < w; j++ ) {
                if ( line[j] == '.' ) {
                    m[i][j] = 0;
                } else if ( line[j] == '#' ) {
                    m[i][j] = 1;
                } else if ( line[j] == 'A' ) {
                    m[i][j] = 2;
                } else if ( line[j] == 'B' ) {
                    m[i][j] = 3;
                }
            }
        }

        string op;
        getline( cin, op );
        int opsize = op.size();

        queue <PIP> Q;
        set <PIP>   V;
        {
            // start node
            Point start, goal;
            for ( int i = 0; i < h; i++ ) for ( int j = 0; j < w; j++ ) {
                if ( m[i][j] == 2 ) {
                    start.r = i;
                    start.c = j;
                    m[i][j] = 0;
                } else if ( m[i][j] == 3 ) {
                    goal.r = i;
                    goal.c = j;
                    m[i][j] = 0;
                }
            }

            PPP init_second( start, goal );
            PIP init( 0, init_second );
            Q.push( init );
            V.insert( init );
        }

        // bfs
        int a_first = -1, a_second, a_third;
        while ( !Q.empty() ) {
            PIP e = Q.front();
            Q.pop();
            int t = e.first;
            Point s = e.second.first, g = e.second.second;

            // is it goal?
            if ( s == g ) {
                a_first = t;
                a_second = s.r;
                a_third = s.c;
                break;
            }

            // move ghost
            int index = t % opsize;
            char c = op[index];
            
            Point next_s = s, next_g = g;
            int next_t = t+1;
            int next_index = ( next_t ) % opsize;
            if ( c == '5' ) {
                // stop
            } else if ( c == '8' ) {
                // north
                next_g.r -= 1;
            } else if ( c == '6' ) {
                // east
                next_g.c += 1;
            } else if ( c == '4' ) { 
                // west
                next_g.c -= 1;
            } else if ( c == '2' ) {
                // south
                next_g.r += 1;
            }
            if ( !check_ghost( next_g ) ) next_g = g;

            // move girl
            int dr[5] = { 0, -1, 0, 0, 1 };
            int dc[5] = { 0, 0, 1, -1, 0 };
            Point backup = next_s;
            for ( int k = 0; k < 5; k++ ) {
                next_s = backup;
                next_s.r += dr[k];
                next_s.c += dc[k];
                PPP next_second( next_s, next_g );
                PIP next_check( next_index, next_second );

                // already visited
                if ( V.count( next_check ) ) continue;
                if ( !check_girl( next_s ) )  continue;
                V.insert( next_check );
                
                PIP next( next_t, next_second );
                Q.push( next );
            }
        } // end of bfs

        if ( a_first == -1 ) {
            cout << "impossible" << endl;
        } else {
            cout << a_first << " " << a_second << " " << a_third << endl;
        }
    }
    return 0;
}