Language: C++

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define INF 1<<20
#define EPS (1e-6)
#define EQ(a,b) (abs((a)-(b)) < EPS)

using namespace std;

typedef long long ll;
typedef complex<double> P;

bool cmp(P a, P b){
	double arga = arg(a);
	double argb = arg(b);
	if(!EQ(arga, argb)){
		return arga < argb;
	} // end if
	
	return abs(a) > abs(b);
}

const int MAX_H = 105;
const int MAX_W = 105;

const int dy[4] = {-1, 0, 1, 0};
const int dx[4] = { 0, 1, 0,-1};
char field[MAX_H][MAX_W];
int d[MAX_H][MAX_W];
int H, W;

bool bfs(char target, int sy, int sx){
	queue<pair<int,int> > que;
	rep (i, MAX_H) rep (j, MAX_W) d[i][j] = INF;
	que.push(make_pair(sy,sx));
	d[sy][sx] = 0;
	
	while(!que.empty()){
		pair<int,int> curr = que.front(); que.pop();
		int cy = curr.first;
		int cx = curr.second;

		// 領域の外に出た
		if (cy == 0 || cy == H + 1 || cx == 0 || cx == W + 1){
			return true;
		} // end if

		rep(k, 4){
			int ny = cy + dy[k];
			int nx = cx + dx[k];
			if (ny < 0 || ny > H + 1 || nx < 0 || nx > W + 1) continue;
			
			if (d[ny][nx] > d[cy][cx] + 1){
				if(
					   ((target == 'O') && (field[ny][nx] == '.'))
					|| ((target == 'X') && (field[ny][nx] != 'O'))
				){
					que.push(make_pair(ny,nx));
					d[ny][nx] = d[cy][cx] + 1;
				} // end if
			} // end if
		} // end rep
	} // end while

	return false;	
}


int main()
{
	memset (field, 0, sizeof(field));
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	rep (i, MAX_H) rep (j, MAX_W) field[i][j] = '.';
	cin >> H >> W;
	vector<string> s(H);
	rep (i, H) cin >> s[i];

	set<pair<int,int> > origin; origin.clear();

	vector<P> p; p.clear();
	for (int i = 1; i <= H; ++i){
		for (int j = 1; j <= W; ++j){
			field[i][j] = s[i-1][j-1];
			if (field[i][j] == 'X'){
				origin.insert(make_pair(i,j));
				P cur;
				cur.real() = j - W / 2.;
				cur.imag() = i - H / 2.;
				p.push_back(cur);
			} // end if
		} // end for
	} // end for

	sort (ALL(p),cmp);
	vector<pair<int,int> > add_shape; add_shape.clear();

	int m = p.size();
	rep (i, m){
		int cy1 = (int)(p[i].imag());
		int cy2 = (int)(p[(i+1) % m].imag());
		int cx1 = (int)(p[i].real());
		int cx2 = (int)(p[(i+1) % m].real());
		int leny = abs(cy1 - cy2);
		int lenx = abs(cx1 - cx2);

		int cnt = 0;
		if (leny > lenx){
			for (int y = cy1; (cy1 <= cy2 ? y < cy2 : y > cy2); y += (cy1 <= cy2 ? +1 : -1), ++cnt);
			for (int dy = 1; dy < cnt; ++dy){
				int dx = ((cx2 - cx1) / cnt) * dy;
				int ny = cy1 + dy * (cy1 <= cy2 ? +1 : -1) + H / 2;
				int nx = cx1 + dx + W/2;
				add_shape.push_back(make_pair(ny,nx));
				field[ny][nx] = 'X';
			} // end rep
		}else{	// if (leny <= lenx)
			for (int x = cx1; (cx1 <= cx2 ? x < cx2 : x > cx2); x += (cx1 <= cx2 ? +1 : -1), ++cnt);
			for (int dx = 1; dx < cnt; ++dx){
				int dy = ((cy2 - cy1) / cnt) * dx;
				int ny = cy1 + dy + H / 2;
				int nx = cx1 + dx * (cx1 <= cx2 ? +1 : -1) + W / 2;
				add_shape.push_back(make_pair(ny,nx));
				field[ny][nx] = 'X';
 			} // end rep
		} // end if
	} // end rep

	for (int i = 1; i <= H; ++i){
		for (int j = 1; j <= W; ++j){
			if (field[i][j] == 'X'){
				rep (k, 4){
					int ny = i + dy[k];
					int nx = j + dx[k];
					if (ny <= 0 || ny >= H + 1 || nx <= 0 || nx >= W + 1) continue;
					if (field[ny][nx] != '.') continue;
					field[ny][nx] = 'O';
				} // end rep
			} // end if
		} // end rep
	} // end rep

	bool escape = false;
	for (int i = 1; i <= H; ++i){
		for (int j = 1; j <= W; ++j){
			if (field[i][j] == 'O'){
				bool curr = bfs(field[i][j], i, j);
				if (!curr) field[i][j] = '.';
			} // end if
		} // end for
	} // end for

	rep (i, add_shape.size()){
		int py = add_shape[i].first;
		int px = add_shape[i].second;
		if (origin.count(make_pair(py,px))) continue;
		field[py][px] = '.';
	} // end rep

	escape = false;
	for (int i = 1; i <= H; ++i){
		for (int j = 1; j <= W; ++j){
			if (field[i][j] == 'X'){
				bool curr = bfs(field[i][j], i, j);
				if (curr) escape |= true;
			} // end if
		} // end for
	} // end for

	int res = 0;
	if (escape){
		res = -1;
	}else{
		for (int i = 1; i <= H; ++i){
			for (int j = 1; j <= W; ++j){
				res += (int)(field[i][j] == 'O');
			} // end for
		} // end for
	} // end if
	cout << res << endl;
	
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:88:41: error: lvalue required as left operand of assignment
   88 |                                 cur.real() = j - W / 2.;
      |                                 ~~~~~~~~^~
a.cc:89:41: error: lvalue required as left operand of assignment
   89 |                                 cur.imag() = i - H / 2.;
      |                                 ~~~~~~~~^~