#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a,b) (a<b?(a=b,1):0)
#define chmin(a,b) (a>b?(a=b,1):0)
#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

struct P {
  int y, x;
  int d;
  int dist;
  P(int y, int x, int d, int dist) :
    y(y),x(x),d(d),dist(dist) {}
  bool operator<(const P &p) const {
    return dist > p.dist;
  }
};

struct Dijkstra {
  int dist[50][50][4];

  void init();
  void transition(const P &p);
  void pawnMove(const vector<pii> &ps, int ky, int kx, int offset);
  bool goal(const P &p) {
    return (p.y==0&&p.x==0);
  }

  
  int &getDist(const P &p) {
    return dist[p.y][p.x][p.d];
  }
  priority_queue<P> Q;  
  void updateQ(const P &p) {
    if (getDist(p) > p.dist) {
      getDist(p) = p.dist;
      Q.push(p);
    }
  }
  int go() {
    while(!Q.empty()) Q.pop();
    int res = -1;
    memset(dist,0x3f,sizeof(dist));
    init();
    while(!Q.empty()) {
      P p = Q.top(); Q.pop();
      if (p.dist > getDist(p)) continue;
      if (goal(p)) {
        res = p.dist;
        break;
      }
      // print(p);
      transition(p);
    }
    return res;
  }
  void print(const P &p);
} dijkstra;

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

const int kdy[4] = {0,0,1,1};
const int kdx[4] = {0,1,0,1};

const int pdy[4][2] = {0,1,2,2,0,1,-1,-1};
const int pdx[4][2] = {2,2,0,1,-1,-1,0,1};

int h,w;
char ba[50][50];

bool ok(int y, int x) {
  return valid(y,x,h,w) && ba[y][x]!='*';
}

bool isKing(int i, int j, int y, int x) {
  REP(k,4) {
    int yy=y+kdy[k];
    int xx=x+kdx[k];
    if (yy==i&xx==j) return 1;
  }
  return 0;
}

int dist2[2][50][50];

void calcPawnDist(int id, int i, int j, int ky, int kx) {
  queue<pii> Q;
  Q.push(pii(i,j));
  dist2[id][i][j] = 0;
  while(!Q.empty()) {
    pii p = Q.front(); Q.pop();
    REP(k,4) {
      int y=p.first+dy[k];
      int x=p.second+dx[k];
      if (!ok(y,x) || isKing(y,x,ky,kx) || dist2[id][y][x]!=-1) continue;
      Q.push(pii(y,x));
      dist2[id][y][x] = dist2[id][p.first][p.second]+1;
    }
  }
}

void Dijkstra::pawnMove(const vector<pii> &ps, int ky, int kx, int distOffset=0) {
  memset(dist2,-1,sizeof(dist2));
  REP(i,2) calcPawnDist(i,ps[i].first,ps[i].second,ky,kx);

  REP(d,4) {
    int cost[2][2];
    bool f = 1;
    REP(i,2) {
      int y=ky+pdy[d][i];
      int x=kx+pdx[d][i];
      if (!ok(y,x)) { f = 0;break; }
      REP(j,2) {
        cost[i][j] = dist2[j][y][x];
        if (cost[i][j] == -1) f = 0;
      }
    }
    if (f) {
      int dis = min(cost[0][0]+cost[1][1], cost[0][1]+cost[1][0]);
      P tmp(ky,kx,d,distOffset+dis);
      updateQ(tmp);
    }
  }
}

void Dijkstra::init() {
  int ky=-1, kx;
  vector<pii> ps;
  REP(i,h)REP(j,w) {
    if (ky==-1 && ba[i][j]=='X') ky = i, kx = j;
    if (ba[i][j] == '.') ps.push_back(pii(i,j));
  }  
  pawnMove(ps,ky,kx);
}

void Dijkstra::transition(const P &p) {
  // pawn
  vector<pii> ps;
  REP(i,2) ps.push_back(pii(p.y+pdy[p.d][i], p.x+pdx[p.d][i]));
  pawnMove(ps,p.y,p.x,p.dist);
  // king
  P nxt(p.y+dy[p.d],p.x+dx[p.d],(p.d+2)%4,p.dist+1);
  updateQ(nxt);
}


void Dijkstra::print(const P &p) {
  cout << p.dist << endl;
  char c[h][w];
  REP(i,h)REP(j,w)c[i][j]=(ba[i][j]=='*'?'*':'o');
  REP(k,4) {
    int y=p.y + kdy[k];
    int x=p.x + kdx[k];
    c[y][x]='X';
  }
  REP(i,2) c[p.y+pdy[p.d][i]][p.x+pdx[p.d][i]] = '.';
  REP(i,h) { REP(j,w) cout << c[i][j]; cout << endl;}
  cout << endl;    
}

int main() {
  while(cin>>h>>w,h||w) {
    REP(i,h)REP(j,w)cin>>ba[i][j];
    if (ba[0][0]=='X') {
      cout << 0 << endl;
      continue;
    }
    cout << dijkstra.go() << endl;
  }
}

Measured memory_bytes: 1292