#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
int n, m;
char mat[505][505];
queue <int> qx;
queue <int> qy;
queue <char> sl;
int dx[] = {0,  0, 1, 0, -1};
int dy[] = {0, -1, 0, 1,  0};
bool posecen[505][505];
bool ima_put = 0;
void bfs (int x, int y, char ch) {
    memset(posecen, 0, sizeof(posecen));
    posecen[x][y] = 1;

    qx.push(x);
    qy.push(y);
    sl.push(ch);

    int nextX;
    int nextY;

    while (qx.size()) {
        mat[qx.front()][qy.front()] = ch;
        for (int kuda = 1; kuda <= 4; kuda++) {
            nextX = qx.front() + dx[kuda];
            nextY = qy.front() + dy[kuda];
            if (ch == 'A' && mat[nextX][nextY] == 'B') {
                ima_put = 1;
                return;
            }
            if (!posecen[nextX][nextY] && mat[nextX][nextY] != 'X' && mat[nextX][nextY] != 'o') {
                posecen[nextX][nextY] = 1;
                qx.push(nextX);
                qy.push(nextY);
                sl.push(ch);
            }
        }
        qx.pop();
        qy.pop();
        sl.pop();
    }
}
void ogradi () {
    for (int i = 1; i <= n; i++) mat[0][i] = mat[n+1][i] = mat[i][0] = mat[i][n+1] = 'o';
}
void print_mat () {
    printf("\n");
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            printf("%c", mat[i][j]);
        }   printf("\n");
    }   printf("\n");
}
struct slog {
    int x;
    int y;
}b_coords, a_coords;
slog find_B (int x, int y) {
    memset(posecen, 0, sizeof(posecen));
    posecen[x][y] = 1;

    qx.push(x);
    qy.push(y);

    int nextX;
    int nextY;

    slog ret;

    while (qx.size()) {
        for (int kuda = 1; kuda <= 4; kuda++) {
            nextX = qx.front() + dx[kuda];
            nextY = qy.front() + dy[kuda];
            if (mat[nextX][nextY] == 'B') {
                ret.x = nextX;
                ret.y = nextY;
                return ret;
            }
            if (!posecen[nextX][nextY] && mat[nextX][nextY] != 'o') {
                if (mat[nextX][nextY] == 'X' && mat[qx.front()][qy.front()] == 'A') {
                    a_coords.x = qx.front();
                    a_coords.y = qy.front();
                }
                posecen[nextX][nextY] = 1;
                qx.push(nextX);
                qy.push(nextY);
            }
        }
        qx.pop();
        qy.pop();
    }
}
int main () {
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> mat[i][j];
        }
    }
    ogradi();
    bfs(1, 1, 'A');
    bfs(n, n, 'B');
//    print_mat();
    if (ima_put) {
        printf("-1\n");
        return 0;
    }
    b_coords = find_B(1, 1);
//    coords.x = 1;
//    coords.y = 2;
//    printf("b: %d %d\n", b_coords.x, b_coords.y);
//    printf("a: %d %d\n", a_coords.x, a_coords.y);
    while (a_coords.y > b_coords.y) {
        if (mat[a_coords.x][a_coords.y-1] == 'X')
            break;
        a_coords.y--;
    }
    while (a_coords.y < b_coords.y) {
        if (mat[a_coords.x][a_coords.y+1] == 'X')
            break;
        a_coords.y++;
    }
    while (a_coords.x > b_coords.x) {
        if (mat[a_coords.x-1][a_coords.y] == 'X')
            break;
        a_coords.x--;
    }
    while (a_coords.x < b_coords.x) {
        if (mat[a_coords.x+1][a_coords.y] == 'X')
            break;
        a_coords.x++;
    }
//    printf("b: %d %d\n", b_coords.x, b_coords.y);
//    printf("a: %d %d\n", a_coords.x, a_coords.y);
    int diffx = abs(b_coords.x - a_coords.x - 1);
    int diffy = abs(b_coords.y - a_coords.y - 1);
    printf("%d\n", max(diffx, diffy));

    return 0;
}
/**
6 1
A.XX..
..XX..
XXXX..
..XX..
..XX..
..XX.B
**/