#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int INF = 1 << 28;
int posxK[4] = { 0, 0, 1, 1 }, posyK[4] = { 0, 1, 0, 1 };
int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
int dxS[4][2] = {
	{ 2, 2 },
	{ 0, 1 },
	{ -1, -1 },
	{ 0, 1 }
};
int dyS[4][2] = {
	{ 0, 1 },
	{ 2, 2 },
	{ 0, 1 },
	{ -1, -1 }
};

int H, W;
string b[50];
int NG[50][50];

struct P{
	int x, y;
};

bool In(int x, int y){
	return 0 <= x && x < W && 0 <= y && y < H;
}

bool InKing(int x, int y){
	for(int i = 0; i < 4; i++){
		if(!In(x + posxK[i], y + posyK[i])) return false;
	}
	return true;
}

bool CheckNG(int x, int y){
	for(int i = 0; i < 4; i++){
		int nx = x + posxK[i], ny = y + posyK[i];
		if(In(nx, ny) && b[ny][nx] == '*') return false;
	}
	return true;
}

void setNG(int kx, int ky){
	for(int i = 0; i < 4; i++){
		NG[ky + posyK[i]][kx + posxK[i]] = 1;
	}
}

void resetNG(int kx, int ky){
	for(int i = 0; i < 4; i++){
		NG[ky + posyK[i]][kx + posxK[i]] = 0;
	}
}


void bfs(int x, int y, int table[50][50]){
	for(int i = 0; i < H; i++){
		for(int j = 0; j < W; j++){
			table[i][j] = INF;
		}
	}
	table[y][x] = 0;
	queue<P> q;
	q.push({ x, y });
	while(q.size()){
		x = q.front().x, y = q.front().y;
		int d = table[y][x];
		q.pop();
		for(int i = 0; i < 4; i++){
			int nx = x + dx[i], ny = y + dy[i];
			if(!In(nx, ny) || NG[ny][nx]) continue;
			if(table[ny][nx] > d + 1){
				table[ny][nx] = d + 1;
				q.push({ nx, ny });
			}
		}
	}
}

struct State{
	P kingpos;
	int dir, cost;
	bool operator < (const State& x) const{
		return cost > x.cost;
	}
};

int sx = -1, sy = -1;
vector<P> sp;
void debug(State s){
	static string b2[50];
	for(int i = 0; i < H; i++){
		b2[i] = b[i];
	}

	for(int i = 0; i < 4; i++){
		int x = s.kingpos.x + posxK[i], y = s.kingpos.y + posyK[i];
		b2[y][x] = 'X';
	}
	if(s.dir == 4){
		for(int i = 0; i < 2; i++){
			b2[sp[i].y][sp[i].x] = '.';
		}
	}
	else{
		int pd = (s.dir + 2) % 4;
		for(int i = 0; i < 2; i++){
			b2[s.kingpos.y + dyS[pd][i]][s.kingpos.x + dxS[pd][i]] = '.';
		}
	}
	for(int i = 0; i < H; i++){
		cout << b2[i] << endl;
	}
	cout << endl;
}

int bfsdist[2][50][50];
int dist[50][50][5];

int dijkstra(int kx, int ky, int sx1, int sy1, int sx2, int sy2){
	if(kx == 0 && ky == 0) return 0;
	int ans = INF;
	fill((int*)begin(dist), (int*)end(dist), INF);
	dist[ky][kx][4] = 0;
	priority_queue<State> q;
	q.push({ {kx, ky}, 4, 0 });
	while(q.size()){
		State s = q.top();
		//cout << "pop" << endl;
		//debug(s);
		P kp = s.kingpos;
		int c = s.cost;
		q.pop();
		if(dist[kp.y][kp.x][s.dir] < c) continue;
		P sp[2];
		if(s.dir == 4){
			sp[0] = { sx1, sy1 };
			sp[1] = { sx2, sy2 };
		}
		else{
			int pd = (s.dir + 2) % 4;
			sp[0] = { kp.x + dxS[pd][0], kp.y + dyS[pd][0] };
			sp[1] = { kp.x + dxS[pd][1], kp.y + dyS[pd][1] };
		}
		setNG(kp.x, kp.y);
		for(int i = 0; i < 2; i++){
			bfs(sp[i].x, sp[i].y, bfsdist[i]);
		}
		for(int i = 0; i < 4; i++){
			int nkx = kp.x + dx[i], nky = kp.y + dy[i];
			if(!InKing(nkx, nky)) continue;
			if(!CheckNG(nkx, nky)) continue;
			int nsx1 = kp.x + dxS[i][0], nsy1 = kp.y + dyS[i][0];
			int nsx2 = kp.x + dxS[i][1], nsy2 = kp.y + dyS[i][1];
			int bfsCost = bfsdist[0][nsy1][nsx1] + bfsdist[1][nsy2][nsx2];
			bfsCost = min(bfsCost, bfsdist[1][nsy1][nsx1] + bfsdist[0][nsy2][nsx2]);
			if(bfsCost >= INF) continue;
			if(dist[nky][nkx][i] > c + bfsCost + 1){
				dist[nky][nkx][i] = c + bfsCost + 1;
				State ns = { { nkx, nky }, i, c + bfsCost + 1 };
				//debug(ns);
				q.push(ns);
				if(nkx == 0 && nky == 0) ans = min(ans, c + bfsCost + 1);
			}
		}
		resetNG(kp.x, kp.y);
	}
	if(ans == INF) ans = -1;
	return ans;
}


int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
#ifdef LOCAL
	std::ifstream in("in");
	std::cin.rdbuf(in.rdbuf());
#endif

	while(cin >> H >> W, H){
		sp.clear();
		sx = -1, sy = -1;
		memset(NG, 0, sizeof NG);
		for(int i = 0; i < H; i++){
			cin >> b[i];
			for(int j = 0; j < W; j++){
				if(b[i][j] == 'X'){
					if(sx == -1) sx = j, sy = i;
					b[i][j] = 'o';
				}
				if(b[i][j] == '.'){
					sp.push_back({ j, i });
					b[i][j] = 'o';
				}
				if(b[i][j] == '*'){
					NG[i][j] = 1;
				}
			}
		}

		int ans = dijkstra(sx, sy, sp[0].x, sp[0].y, sp[1].x, sp[1].y);
		cout << ans << endl;
	}
}

Measured memory_bytes: 3240