#include <iostream>
#include <queue>
#include <cmath>
using namespace std;
const int MAX_SIZE = 75;
int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
class Point
{
public:
int x,y,step,times;
};
class Game
{
public:
void input(int,int);
void order(int,int,int,int);
int bfs();
private:
int Width, Height;
int lenof[MAX_SIZE+2][MAX_SIZE+2];
bool hash[MAX_SIZE+2][MAX_SIZE+2];
char map[MAX_SIZE+2][MAX_SIZE+2];
Point Start, End;
};
void Game::input(int w, int h)
{
int i, j;
Width = w;
Height = h;
memset(hash, 0, sizeof(hash));
for (i = 1; i <= w; ++i)
{
getchar();
for (j = 1; j <= h; ++j)
{
scanf("%c", &map[i][j]);
if (map[i][j] == 'X')
{
hash[i][j] = 1;
}
}
}
}
void Game::order(int x1, int y1, int x2, int y2)
{
Start.x = x1;
Start.y = y1;
End.x = x2;
End.y = y2;
memset(lenof, 4, sizeof(lenof));
int t1 = hash[x1][y1];
int t2 = hash[x2][y2];
hash[x1][y1] = hash[x2][y2] = 0;
int temp = bfs();
if (temp == -1)
{
printf("impossible.\n");
}
else
{
printf("%d segments.\n", temp);
}
hash[x1][y1] = t1;
hash[x2][y2] = t2;
}
int Game::bfs()
{
int i;
Point first, next;
first = Start;
first.times = 0;
queue <Point> Q;
for (i = 0; i < 4; ++i)
{
first.step = i;
Q.push(first);
}
lenof[Start.x][Start.y] = 0;
while (!Q.empty())
{
first = Q.front();
Q.pop();
for (i = 0; i < 4; ++i)
{
next.x = first.x + dir[i][0];
next.y = first.y + dir[i][1];
if (next.x >= 0 && next.x <= Width + 1 && next.y >= 0 && next.y <= Height + 1
&& !hash[next.x][next.y])
{
next.step = i;
if (first.step == next.step)
{
if (first.times <= lenof[next.x][next.y])
{
lenof[next.x][next.y] = first.times;
next.times = first.times;
Q.push(next);
}
}
else if (abs(first.step - next.step) != 2)
{
if (first.times + 1 <= lenof[next.x][next.y])
{
lenof[next.x][next.y] = first.times + 1;
next.times = first.times + 1;
Q.push(next);
}
}
}
}
}
if (lenof[End.x][End.y] < 60000000)
{
return lenof[End.x][End.y] + 1;
}
else
{
return -1;
}
}
int main()
{
int a, b, cnt = 0;
while (scanf("%d %d", &b, &a) != EOF && !(!a && !b))
{
Game g;
g.input(a, b);
printf("Board #%d:\n", ++cnt);
int x1,x2,y1,y2;
int cntcnt = 0;
while (scanf("%d %d %d %d", &y1, &x1, &y2, &x2) && !(!x1 && !x2 && !y1 && !y2))
{
printf("Pair %d: ", ++cntcnt);
g.order(x1,y1,x2,y2);
}
printf("\n");
}
return 0;
}