#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define MAX_N (1<<19)
#define INF 100000000
#define MOD 1000000007
#define RANK 1000000000
#define EPS 1e-10
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int h, w;
char b[50][50];
int st[2];
int pos;
int dp[4*50*50*50+50*50+50];
int dis[2][50*50+50];
vector<P> e[4*50*50+50*50+50];
queue<int> que;
priority_queue<P, vector<P>, greater<P> > pque;
int dy[4] = {1,0,-1,0};
int dx[4] = {0,1,0,-1};
int ddy[4][2] = {{1,1},{0,-1},{-2,-2},{0,-1}};
int ddx[4][2] = {{0,-1},{1,1},{0,-1},{-2,-2}};

void bfs(int now, int id, int p){
    rep(i,50*50+50) dis[id][i] = INF;
    dis[id][now] = 0;
    que.push(now);
    while(!que.empty()){
        int y = que.front()/50, x = que.front()%50;
        que.pop();
        rep(i,4){
            int yy = y+dy[i], xx = x+dx[i];
            if(0>yy||yy>=h||0>xx||xx>=w) continue;
            if(b[yy][xx] == '*') continue;
            if(yy == p/50 && xx == p%50) continue;
            if(yy == p/50-1 && xx == p%50) continue;
            if(yy == p/50 && xx == p%50-1) continue;
            if(yy == p/50-1 && xx == p%50-1) continue;
            if(dis[id][yy*50+xx] != INF) continue;
            dis[id][yy*50+xx] = dis[id][y*50+x]+1;
            que.push(yy*50+xx);
        }
    }
}

bool inrange(int y, int x){
    if(b[y][x] == '*') return false;
    if(b[y-1][x] == '*') return false;
    if(b[y][x-1] == '*') return false;
    if(b[y-1][x-1] == '*') return false;
    return true;
}

void init(){
    rep(i,2){
        bfs(st[i],i,pos);
    }
    int py = pos/50, px = pos%50;
    rep(i,4){
        if(0>py+ddy[i][0]||py+ddy[i][0]>=h||0>px+ddx[i][0]||px+ddx[i][0]>=w) continue;
        if(0>py+ddy[i][1]||py+ddy[i][1]>=h||0>px+ddx[i][1]||px+ddx[i][1]>=w) continue;
        int d1 = dis[0][(py+ddy[i][0])*50+px+ddx[i][0]]+dis[1][(py+ddy[i][1])*50+px+ddx[i][1]];
        int d2 = dis[0][(py+ddy[i][1])*50+px+ddx[i][1]]+dis[1][(py+ddy[i][0])*50+px+ddx[i][0]];
        d1 = min(d1,d2);
        if(d1 >= INF) continue;
        pque.push(P(d1,i*50*50+py*50+px));
        dp[i*50*50+py*50+px] = d1;
    }
    for(int i = 1; i < h; i++) for(int j = 1; j < w; j++) rep(k,4){
        if(!inrange(i,j)) continue;
        if(0>i+ddy[k][0]||i+ddy[k][0]>=h||0>j+ddx[k][0]||j+ddx[k][0]>=w) continue;
        if(0>i+ddy[k][1]||i+ddy[k][1]>=h||0>j+ddx[k][1]||j+ddx[k][1]>=w) continue;
        if(b[i+ddy[k][0]][j+ddx[k][0]] == '*') continue;
        if(b[i+ddy[k][1]][j+ddx[k][1]] == '*') continue;
        bfs((i+ddy[k][0])*50+j+ddx[k][0],0,i*50+j);
        bfs((i+ddy[k][1])*50+j+ddx[k][1],1,i*50+j);
        rep(l,4){
            if(k == l) continue;
            if(0>i+ddy[k][0]||i+ddy[k][0]>=h||0>j+ddx[k][0]||j+ddx[k][0]>=w) continue;
            if(0>i+ddy[k][1]||i+ddy[k][1]>=h||0>j+ddx[k][1]||j+ddx[k][1]>=w) continue;
            if(b[i+ddy[k][0]][j+ddx[k][0]] == '*') continue;
            if(b[i+ddy[k][1]][j+ddx[k][1]] == '*') continue;
            int d1 = dis[0][(i+ddy[l][0])*50+j+ddx[l][0]]+dis[1][(i+ddy[l][1])*50+j+ddx[l][1]];
            int d2 = dis[0][(i+ddy[l][1])*50+j+ddx[l][1]]+dis[1][(i+ddy[l][0])*50+j+ddx[l][0]];
            d1 = min(d1,d2);
            if(d1 >= INF) continue;
            e[k*50*50+i*50+j].push_back(P(l*50*50+i*50+j,d1));
        }
    }
}

void solve(){
    rep(i,h) rep(j,w) cin >> b[i][j];
    while(!pque.empty()) pque.pop();
    rep(i,4*50*50+50*50+50) e[i].clear();
    rep(i,4*50*50*50+50*50+50) dp[i] = INF;
    int cnt = 0;
    rep(i,h) rep(j,w) if(b[i][j] == '.'){
        st[cnt++] = i*50+j;
        b[i][j] = 'o';
    }
    rep(i,h) rep(j,w) if(b[i][j] == 'X'){
        pos = 50*i+j;
        b[i][j] = 'o';
    }
    if(pos == 51){
        cout << 0 << endl;
        return;
    }
    rep(i,4){
        int ii = (i+2)%4;
        for(int j = 1; j < h; j++){
            int jj = j+dy[i];
            for(int k = 1; k < w; k++){
                int kk = k+dx[i];
                if(1>jj||jj>=h||1>kk||kk>=w) continue;
                if(!inrange(j,k)) continue;
                if(!inrange(jj,kk)) continue;
                e[i*50*50+j*50+k].push_back(P(ii*50*50+jj*50+kk,1));
            }
        }
    }
    init();
    while(!pque.empty()){
        P p = pque.top();
        pque.pop();
        if(p.first > dp[p.second]) continue;
        rep(i,e[p.second].size()){
            P to = e[p.second][i];
            if(dp[to.first] > dp[p.second]+to.second){
                dp[to.first] = dp[p.second]+to.second;
                pque.push(P(dp[to.first],to.first));
            }
        }
    }
    int ans = INF;
    rep(i,4) ans = min(ans,dp[i*50*50+1*50+1]);
    if(ans == INF) ans = -1;
    cout << ans << endl;
}

int main(){
    while(cin >> h >> w){
        if(h == 0 && w == 0) break;
        solve();
    }
}

Measured memory_bytes: 5496