#include<bits/stdc++.h>
using namespace std;
using Int = long long;
Int dp[55][55][4];
Int mv[55][55][4][4];
Int di[8][55][55];
const Int INF = 1e9;
signed main(){
  Int n,m;
  while(cin>>n>>m,n){
    vector<string> s(n);
    for(Int i=0;i<n;i++) cin>>s[i];
    auto in=[&](Int y,Int x){
      return 0<=y&&y<n&&0<=x&&x<m;
    };
    using P = pair<Int, Int>;
    P st;
    vector<P> sp;
    for(Int i=0;i<n;i++){
      for(Int j=0;j<m;j++){
	if(s[i][j]=='X'){
	  assert(s[i][j+1]=='X');
	  assert(s[i+1][j]=='X');
	  assert(s[i+1][j+1]=='X');
	  s[i][j]=s[i][j+1]='o';
	  s[i+1][j]=s[i+1][j+1]='o';
	  st=P(i,j);
	}
	if(s[i][j]=='.') sp.emplace_back(i,j);
      }
    }
    if(st==P(0,0)){
      cout<<0<<endl;
      continue;
    }
    
    //cout<<st.first<<" "<<st.second<<endl;
    //cout<<sp.size()<<endl;
    //for(int i=0;i<n;i++) cout<<s[i]<<endl;cout<<endl;
    
    auto bfs=[&](Int sy,Int sx){
      Int ay[]={-1,-1,0,1,2,2,1,0};
      Int ax[]={0,1,2,2,1,0,-1,-1};

      if(s[sy][sx]=='*'||
	 !in(sy,sx+1)||s[sy][sx+1]=='*'||
	 !in(sy+1,sx)||s[sy+1][sx]=='*'||
	 !in(sy+1,sx+1)||s[sy+1][sx+1]=='*') return;
      
      s[sy][sx]=s[sy][sx+1]='*';
      s[sy+1][sx]=s[sy+1][sx+1]='*';
      
      for(Int k=0;k<8;k++){
	Int y=sy+ay[k],x=sx+ax[k];
	for(Int i=0;i<55;i++)
	  for(Int j=0;j<55;j++)
	    di[k][i][j]=INF;
	if(!in(y,x)||s[y][x]=='*') continue;
	queue<P> q;
	q.emplace(y,x);
	di[k][y][x]=0;
	Int dy[]={0,1,0,-1};
	Int dx[]={1,0,-1,0};
	while(!q.empty()){
	  P p=q.front();q.pop();
	  y=p.first;x=p.second;
	  for(Int i=0;i<4;i++){
	    Int ny=y+dy[i],nx=x+dx[i];
	    if(!in(ny,nx)||s[ny][nx]=='*'||di[k][ny][nx]<=di[k][y][x]+1) continue;
	    di[k][ny][nx]=di[k][y][x]+1;
	    q.emplace(ny,nx);
	  }
	}
      }
      
      for(Int i=0;i<4;i++){
	for(Int j=0;j<4;j++){
	  if(!in(sy+ay[j*2],sx+ax[j*2])||!in(sy+ay[j*2+1],sx+ax[j*2+1])) continue;
	  Int a=di[i*2][sy+ay[j*2]][sx+ax[j*2]]+di[i*2+1][sy+ay[j*2+1]][sx+ax[j*2+1]];
	  Int b=di[i*2][sy+ay[j*2+1]][sx+ax[j*2+1]]+di[i*2+1][sy+ay[j*2]][sx+ax[j*2]];
	  mv[sy][sx][i][j]=min(a,b);
	  //cout<<sy<<" "<<sx<<":"<<i<<" "<<j<<":"<<mv[sy][sx][i][j]<<endl;;
	}
      }
      
      s[sy][sx]=s[sy][sx+1]='o';
      s[sy+1][sx]=s[sy+1][sx+1]='o';
    };

    for(Int i=0;i<55;i++)
      for(Int j=0;j<55;j++)
	for(Int k=0;k<4;k++){
	  dp[i][j][k]=INF;
	  for(Int l=0;l<4;l++)
	    mv[i][j][k][l]=INF;
	}
	  
    for(Int i=0;i<n;i++)
      for(Int j=0;j<m;j++)
	bfs(i,j);
    
    bfs(st.first,st.second);

    using T = tuple<Int, Int, Int, Int>;
    priority_queue<T,vector<T>,greater<T> > pq;
    for(Int i=0;i<4;i++){
      Int a=di[i*2][sp[0].first][sp[0].second]+di[i*2+1][sp[1].first][sp[1].second];
      Int b=di[i*2][sp[1].first][sp[1].second]+di[i*2+1][sp[0].first][sp[0].second];
      dp[st.first][st.second][i]=min(a,b);
      if(dp[st.first][st.second][i]<INF)
	pq.emplace(dp[st.first][st.second][i],st.first,st.second,i);
    }
    while(!pq.empty()){
      int d,y,x,k;
      tie(d,y,x,k)=pq.top();pq.pop();
      if(dp[y][x][k]<d) continue;
      //cout<<y<<" "<<x<<" "<<k<<":"<<d<<endl;
      int dy[]={-1,0,1,0};
      int dx[]={0,1,0,-1};
      {
	int ny=y+dy[k],nx=x+dx[k],nk=(k+2)%4;
	if(dp[y][x][k]+1<dp[ny][nx][nk]){
	  dp[ny][nx][nk]=dp[y][x][k]+1;
	  pq.emplace(dp[ny][nx][nk],ny,nx,nk);
	}
      }
      for(int i=0;i<4;i++){
	int c=mv[y][x][k][i];
	//cout<<i<<":"<<dp[y][x][i]<<" "<<dp[y][x][k]+c<<endl;
	if(dp[y][x][i]<=dp[y][x][k]+c) continue;
	dp[y][x][i]=dp[y][x][k]+c;
	pq.emplace(dp[y][x][i],y,x,i);
      }
    }
    
    Int ans=min(dp[0][0][1],dp[0][0][2]);
    if(ans==INF) ans=-1;
    cout<<ans<<endl;
  }
  return 0;
}

Measured memory_bytes: 3804