#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (1e+18) + 7;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-6;
const ld pi = acos(-1.0);
//typedef vector<vector<ll>> mat;
typedef vector<int> vec;

ll mod_pow(ll a, ll n) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * a%mod;
		a = a * a%mod; n >>= 1;
	}
	return res;
}

struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n%mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint &a, modint b) { a.n = ((ll)a.n*b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, int n) {
	if (n == 0)return modint(1);
	modint res = (a*a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 500;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}

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

char mp[50][50];
bool b[50][50];

struct edge {
	int to, cost;
};

vector<edge> G[10000];

int h, w;
int dist[50][50][2];
bool isok(int sx, int sy) {
	if (sx < 0 || sy < 0 || sx >= h || sy >= w)return false;
	return true;
}
bool yaru(int sx,int sy,int id) {
	if (sx < 0 || sy < 0 || sx >= h || sy >= w || b[sx][sy])return false;
	rep(i, h)rep(j, w)dist[i][j][id] = mod;
	queue<P> q;
	dist[sx][sy][id] = 0; q.push({ sx,sy });
	while (!q.empty()) {
		P p = q.front(); q.pop();
		rep(j, 4) {
			int nx = p.first + dx[j];
			int ny = p.second + dy[j];
			if (nx < 0 || nx >= h || ny < 0 || ny >= w)continue;
			if (b[nx][ny])continue;
			if (dist[p.first][p.second][id] + 1 < dist[nx][ny][id]) {
				dist[nx][ny][id] = dist[p.first][p.second][id] + 1;
				q.push({ nx,ny });
			}
		}
	}
	return true;
}

int cx[8] = { 0,1,2,2,0,1,-1,-1 };
int cy[8] = { 2,2,0,1,-1,-1,0,1 };

int dp[10000];
void solve() {
	rep(i, 10000) {
		G[i].clear(); dp[i] = mod;
	}
	rep(i, h)rep(j, w) {
		cin >> mp[i][j];
		b[i][j] = (mp[i][j] == '*');
	}
	rep(i, h - 1) {
		rep(j, w - 1) {
			bool valid = true;
			rep(x, 2)rep(y, 2)if (b[i + x][j + y])valid = false;
			if (!valid)continue;
			rep(x, 2)rep(y, 2)b[i + x][j + y] = true;

			rep(l, 4) {
				int sx[2]; int sy[2];
				valid = true;
				rep(t, 2) {
					sx[t] = i + cx[l * 2 + t];
					sy[t] = j + cy[l * 2 + t];
					if (!yaru(sx[t], sy[t],t))valid = false;
				}
				if (!valid)continue;
				int sid = i * (w - 1) + j + l * (h - 1)*(w - 1);
				int ni = i + dx[l], nj = j + dy[l];
				int tid = ni * (w - 1) + nj + (l ^ 2)*(h - 1)*(w - 1);
				G[sid].push_back({ tid,1 });
				G[tid].push_back({ sid,1 });
				
				Rep(m, l + 1, 4) {
					int gx[2], gy[2];
					valid = true;
					rep(t, 2) {
						gx[t] = i + cx[m * 2 + t];
						gy[t] = j + cy[m * 2 + t];
						if (!isok(gx[t], gy[t]))valid = false;
					}
					if (!valid)continue;
					int cost = mod;
					rep(t, 2) {
						cost = min(cost, dist[gx[0]][gy[0]][t] + dist[gx[1]][gy[1]][t ^ 1]);
					}
					if (cost != mod) {
						int sid = i * (w - 1) + j + l * (h - 1)*(w - 1);
						int tid = i * (w - 1) + j + m * (h - 1)*(w - 1);
						G[sid].push_back({ tid,cost });
						G[tid].push_back({ sid,cost });
					}
				}
			}
			rep(x, 2)rep(y, 2)b[i + x][j + y] = false;
		}
	}
	priority_queue<P, vector<P>, greater<P>> q;
	vector<P> v;
	rep(i, h)rep(j, w) {
		if (mp[i][j] == '.') {
			v.push_back({ i,j });
		}
	}
	int lx, ly;
	per(i, h)per(j, w) {
		if (mp[i][j] == 'X') {
			lx = i, ly = j;
		}
	}
	rep(x, 2)rep(y, 2)b[lx + x][ly + y] = true;
	rep(t, 2)yaru(v[t].first, v[t].second, t);
	rep(x, 2)rep(y, 2)b[lx + x][ly + y] = false;
	
	if (lx == 0 && ly == 0) {
		cout << 0 << endl; return;
	}
	rep(l, 4) {
		int sx[2]; int sy[2];
		bool valid = true;
		rep(t, 2) {
			sx[t] = lx + cx[l * 2 + t];
			sy[t] = ly + cy[l * 2 + t];
			if (!isok(sx[t], sy[t]))valid = false;
		}
		if (!valid)continue;
		int cost = mod;
		rep(t, 2) {
			cost = min(cost, dist[sx[0]][sy[0]][t] + dist[sx[1]][sy[1]][1 ^ t]);
		}
		int tid = lx * (w - 1) + ly + l * (h - 1)*(w - 1);
		dp[tid] = cost;
		q.push({ cost,tid });
	}
	while (!q.empty()) {
		P p = q.top(); q.pop();
		int id = p.second;
		if (dp[id] < p.first)continue;
		for (edge e : G[id]) {
			int nd = e.cost + dp[id];
			if (nd < dp[e.to]) {
				dp[e.to] = nd;
				q.push({ nd,e.to });
			}
		}
	}
	int ans = min(dp[(h - 1)*(w - 1)], dp[0]);
	if (ans == mod) {
		ans = -1;
	}
	cout << ans << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(12);
	//init_f(); init();
	//int t; cin >> t; rep(i, t)solve();
	while (cin >>h>>w,h)solve();
	//solve();
	stop
		return 0;
}



Measured memory_bytes: 3672