Language: C++

#include "bits/stdc++.h"
using namespace std;

//諸機能
#pragma region MACRO 
#define putans(x)  std::cerr << "answer: " ; cout << (x) << endl
#define dputans(x) std::cerr << "answer: "; cout << setprecision(13) << (double)(x) << endl
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define RREP(i,a,n) for(int i=(int)(n-1); i>= a; i--)
#define rep(i,n) REP(i,0,n)
#define rrep(i,n) RREP(i,0,n)
#define all(a) begin((a)),end((a))
#define mp make_pair
#define exist(container, n) ((container).find((n)) != (container).end())
#define equals(a,b) (fabs((a)-(b)) < EPS)
#ifdef _DEBUG //ファイルからテストデータを読み込む
std::ifstream ifs("data.txt");
#define put ifs >>
#else //ジャッジシステムでいい感じにやる
#define put cin >>
#endif
#pragma endregion

//デバッグなどの支援
#pragma region CODING_SUPPORT
#define dbg(var0) { std::cerr << ( #var0 ) << "=" << ( var0 ) << endl; }
#define dbg2(var0, var1) { std::cerr << ( #var0 ) << "=" << ( var0 ) << ", "; dbg(var1); }
#define dbg3(var0, var1, var2) { std::cerr << ( #var0 ) << "=" << ( var0 ) << ", "; dbg2(var1, var2); }
#define dbgArray(a,n) {std::cerr << (#a) << "=";  rep(i,n){std::cerr <<(a[i])<<",";} cerr<<endl;} 
#ifndef _DEBUG
#define dbg1 {}
#define dbg2 {}
#define dbg3 {}
#define dbgArray {}
#endif 
#pragma endregion 
//typedef（書き換える、書き足す可能性ある）
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii; typedef pair<string, string> pss; typedef pair<int, string>pis;
typedef vector<string> vs; typedef vector<int> vi;
#pragma endregion
//諸々の定数(書き換える可能性ある)
#pragma region CONST_VAL
#define PI (2*acos(0.0))
#define EPS (1e-9)
#define MOD (ll)(1e9 + 7)
#define INF (ll)(1e9)
#pragma endregion

class Point {//幾何上のべクトル
public:
	double x, y;
	Point(double x = 0, double y = 0) : x(x), y(y) {}

	Point operator + (Point p) { return Point(x + p.x, y + p.y); }
	Point operator - (Point p) { return Point(x - p.x, y - p.y); }
	Point operator * (double a) { return Point(x * a, y * a); }
	double operator * (Point p) { return dot(p); }
	Point operator / (double a) { return Point(x / a, y / a); }

	double abs() { return sqrt(norm()); }
	double norm() { return x*x + y*y; }

	bool operator < (const Point &p) const { return  x != p.x ? x < p.x : y < p.y; }
	bool operator == (const Point &p) const { return  fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; }
	//内積、外積
	double dot(Point p) { return x * p.x + y * p.y; }
	double cross(Point p) { return x * p.y - y * p.x; }


};
class Circle {
public:
	Point c;
	double r;
	Circle(Point c = Point(), double r = 0.0) :c(c), r(r) {}
};
struct Segment {//線分
	Point p1, p2;
};

typedef Point Vector;
typedef Segment Line;//直線
typedef vector<Point> Polygon;

double v_norm(Point p) { return p.x * p.x + p.y * p.y; }
double abs(Point p) { return v_norm(p); }
double dot(Point p, Point q) { return p.x * q.x + p.y * q.y; }
double cross(Point p, Point q) { return q.x * p.y - p.y * p.x; }





//直行、並行
bool isOrthogonal(Vector a, Vector b) { return equals(a*b, 0.0); }
bool isParallel(Vector a, Vector b) { return equals(cross(a, b), 0.0); }

//線分sに対する点pの射影
Point projection(Segment s, Point p) {
	Vector base = s.p2 - s.p1;
	double r = dot(( p - s.p1 ), base) / v_norm(base);
	return s.p1 + base*r;
}
//線分sに対する点pの反射
Point reflection(Segment s, Point p) { return p + ( projection(s, p) - p ) * 2; }




//double getDistance(Point a, Point b) { return ( a - b ).abs; }
//double getDistanceLP(Line l, Point p) { return abs(cross(l.p2 - l.p1, p - l.p1) / abs(l.p2 - l.p1)); }
//double getDistanceSP(Segment s, Point p) {
//	if (dot(s.p2 - s.p1, p - s.p1) < 0.0) return abs(p - s.p1);
//	if (dot(s.p1 - s.p2, p - s.p2) < 0.0) return abs(p - s.p2);
//}

//double getDistance(Segment s1, Segment s2) {
//	if (intersect(s1, s2)) return 0;
//	return min(min(getDistanceSP(s1, s2.p1), getDistanceSP(s1, s2.p2))
//	         , min(getDistanceSP(s2, s1.p1), getDistanceSP(s2, s1.p2));
//}

//p1-p0を基準として p2-p0 の2ベクトルについて
static const int COUNTER_CLOCKWISE = 1;//反時計回り
static const int CLOCKWISE = -1;//時計回り
static const int ONLINE_BACK = 2;//同一直線状p2,p0,p1の順
static const int ONLINE_FRONT = 1;//同一直線上p0,p1,p2の順
static const int ON_SEGMENT = 0;//p2がp0,p1上にある場合
int ccw(Point p0, Point p1, Point p2) {//上記の分類関数
	Vector a = p1 - p0;
	Vector b = p2 - p0;
	if (cross(a, b) > EPS) return COUNTER_CLOCKWISE;
	if (cross(a, b) < EPS) return CLOCKWISE;
	if (dot(a, b) < -EPS) return ONLINE_BACK;
	if (v_norm(a) < v_norm(b)) return ONLINE_FRONT;
	return ON_SEGMENT;
}
////線分p1p2とp3p4の交差判定
//bool intersect(Point p1, Point p2, Point p3, Point p4) {return ( (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0) && (ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0));}
//bool intersect(Segment s1, Segment s2) {return intersect(s1.p1 , s1.p1, s2.p1, s2.p2);}
//
//Point getCrossPoint(Segment s1, Segment s2) {
//	Vector base = s2.p2 - s2.p1;
//	double d1 = abs(cross(base , s1.p1 - s2.p1));
//	double d2 = abs(cross(base , s1.p2 - s2.p1));
//	double t = d1 / ( d1 + d2 );
//	return s1.p1 + (s1.p2 - s1.p1 ) * t;
//}
//
////直線と円の交点を調べる
//pair<Point, Point> getCrossPoints(Circle c, Line l) {
//	//assert(intersect(c, i)); 交差しない場合についてのはず
//	Vector pr = projection(c, l);
//	Vector e = ( l.p2 - l.p1 ) / abs(l.p2 - l.p1);
//	double base = sqrt(c.r * c.r - norm(pr - c.c));
//	return mp(pr + e * base, pr - e * base);
//}
//
////点の内包を調べる
//static const int IN_POLYGON = 2;//多角形の内部
//static const int OUT_POLYGON = 1;//多角形の外部
//static const int ON_POLYGON = 0;//多角形の線分上
//int contains(Polygon g, Point p) {
//	int n = g.size();
//	bool x = false;
//	rep(i, n) {
//		Point a = g[i] - p, b = g[( i + 1 ) % n] - p;
//		if (abs(cross(a, b)) < EPS && dot(a, b) < EPS) return OUT_POLYGON;
//		if (a.y > b.y) swap(a, b);
//		if (a.y < EPS && EPS < b.y && cross(a, b)) x = !x;
//	}
//	return ( x ? IN_POLYGON : ON_POLYGON );
//}

//凸包
Polygon ConvexHull(Polygon s) {
	Polygon u, l;
	if (s.size() < 3) return s;
	sort(all(s));
	u.push_back(s[0]);
	u.push_back(s[1]);
	l.push_back(s[s.size() - 1]);
	l.push_back(s[s.size() - 2]);

	for (int i = 2; i < s.size(); i++) {
		for (int n = u.size(); n >= 2 && ccw(u[n - 2], u[n - 1], s[i]) != CLOCKWISE; n--)u.pop_back();
		u.push_back(s[i]);
	}

	for (int i = s.size()-3; i >= 0; i--) {
		for (int n = l.size(); n >= 2 && ccw(l[n - 2], l[n - 1], s[i]) != CLOCKWISE; n--)l.pop_back();
		l.push_back(s[i]);
	}

	reverse(all(l));
	for (int i = u.size() - 2; i >= 1; i--) l.push_back(u[i]);
	return l;
}

/*
#define i(x) int x; scanf("%d",&x);
#define l(x) ll x; scanf("%lld",&x);
#define d(x) double  x; scanf("%lf",&x);
*/
//今度実装がんばる
//https://www23.atwiki.jp/akitaicpc/pages/65.html


int main() {
	/*double xp1, yp1, xp2, yp2; put xp1 >> yp1 >> xp2 >> yp2;
	Segment s;
	s.p1 = Point(xp1, yp1);
	s.p2 = Point(xp2, yp2);

	int n; put n;
	rep(i, n) {
		Point p;
		put p.x >> p.y;
		Point prj =reflection(s, p);
		cout << setprecision(13) << prj.x << " " << prj.y << endl;
	}*/
	//テンプレート化を来世に考える
	auto getI = [&]()->auto { int tmp; put tmp; return tmp; };
	auto getLL = [&]()->auto { ll tmp; put tmp; return tmp; };
	auto getS = [&]()->auto { string tmp; put tmp; return tmp; };
	Polygon sheep;
	Polygon convex_sheep;
	int h, w; put h >> w;
	rep(i, h) {
		string s; put s;
		rep(j, s.size()) {
			if (s[j] == 'X') {
				if (i == 0 || j == 0) {
					putans(-1);
					goto END;
				}
				sheep.push_back(Point(i, j));
			}
		}
	}
	convex_sheep = ConvexHull(sheep);
	int num = 0;
	rep(i, convex_sheep.size()) {
		Point s = convex_sheep[i % convex_sheep.size()];
		Point g = convex_sheep[(i + 1) % convex_sheep.size()];
		int mandist = abs(s.x - g.x) + abs(s.y - g.y);
		num += mandist;
	}
	putans(num);
END:
	return 0;
}

//
//int n, a, b; put n >> a >> b;
//	vi t;
//	rep(i, n) {
//		t.push_back(get());
//	}
//	int count = 0;
//	rep(i, n) {
//		if (t[i] < a || b <= t[i])count++;
//	}
//	putans(count);

//
//	set<string> list;
//	int ans = 0;
//	int n; put n;
//	int k; put k;
//	int numAlphabet[26];
//	fill(all(numAlphabet), 0);
//	rep(i, n) {
//		string s; put s;
//		if (exist(list, s))continue;
//		list.insert(s);
//		numAlphabet[s[0] - 'A']++;
//	}
//	sort(all(numAlphabet),greater<int>());
//	while (true) {
//		rep(i, k) {
//			if (numAlphabet[i] == 0) goto END;
//			else numAlphabet[i]--;
//		}
//		sort(all(numAlphabet), greater<int>());
//		ans++;
//	}
//END:
//	putans(ans);


	//int t; put t;
	//rep(i, t) {
	//	int n, d; put n >> d;
	//	if (n == 1) {
	//		putans(d);
	//		continue;
	//	}
	//	putans(n % 2 != 0 ? ( n - 1 ) * 127 + d : ( n - 1 ) * 127 + ( 127 ^ d ));
	//}


//
//int n; put n;
//string s1, s2;
//string r;
//string patan1[4] = { ".","#",".","#" };
//string patan2[4] = { ".",".","#","#" };
//bool front = true;
//rep(j, 420 / 4) {
//	if (front) {
//		rep(i, 4) {
//			cout << s1 + patan1[i] + "\n" << s2 + patan2[i] << endl;
//			cin >> r;
//			if (r == "T") {
//				s1 += patan1[i];
//				s2 += patan2[i];
//				break;
//			}
//			if (r == "F" && i == 3) {
//				front = !front;
//			}
//			if (r == "end")goto END;
//		}
//	}
//	else {
//		rep(i, 4) {
//			cout << patan1[i] + s1 + "\n" << patan2[i] + s2 << endl;
//			cin >> r;
//			if (r == "T") {
//				s1 = patan1[i] + s1;
//				s2 = patan2[i] + s2;
//				break;
//			}
//			if (r == "F" && i == 3) {
//				front = !front;
//			}
//			if (r == "end")goto END;
//		}
//	}
//
//}
//END:
//cout << endl;

Compiler output:
a.cc:32:9: warning: "dbg2" redefined
   32 | #define dbg2 {}
      |         ^~~~
a.cc:27:9: note: this is the location of the previous definition
   27 | #define dbg2(var0, var1) { std::cerr << ( #var0 ) << "=" << ( var0 ) << ", "; dbg(var1); }
      |         ^~~~
a.cc:33:9: warning: "dbg3" redefined
   33 | #define dbg3 {}
      |         ^~~~
a.cc:28:9: note: this is the location of the previous definition
   28 | #define dbg3(var0, var1, var2) { std::cerr << ( #var0 ) << "=" << ( var0 ) << ", "; dbg2(var1, var2); }
      |         ^~~~
a.cc:34:9: warning: "dbgArray" redefined
   34 | #define dbgArray {}
      |         ^~~~~~~~
a.cc:29:9: note: this is the location of the previous definition
   29 | #define dbgArray(a,n) {std::cerr << (#a) << "=";  rep(i,n){std::cerr <<(a[i])<<",";} cerr<<endl;}
      |         ^~~~~~~~
a.cc: In function 'int main()':
a.cc:252:1: error: jump to label 'END'
  252 | END:
      | ^~~
a.cc:237:46: note:   from here
  237 |                                         goto END;
      |                                              ^~~
a.cc:244:13: note:   crosses initialization of 'int num'
  244 |         int num = 0;
      |             ^~~