//#pragma warning(disable:4996)
//#include <Windows.h>
#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <random>
#include <functional>
#include <stack>
#include <iomanip>
#include <cassert>
//#include <boost/multiprecision/cpp_int.hpp>
#include <complex>
#include <cstdio>
#include <list>
#include <bitset>
//#include <stdio.h>

//< in.txt > out.txt
using namespace std;
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
typedef long long LL;
typedef long double LD;
//typedef boost::multiprecision::cpp_int bigint;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PI;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef unsigned long long ULL;

template<class T>
inline void chmin(T& a, T b) {
	a = min(a, b);
}

template<class T>
inline void chmax(T& a, T b) {
	a = max(a, b);
}

void input();
void solve();

void daminput();

int main() {
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	input();
	//daminput();
	solve();
	return 0;
}

//////////////////////////////////////////////////
//////////////////////////////////////////////////

int H, W;
vector<string> S;

void input() {
	cin >> H >> W;
	S.resize(H);
	for (int y = 0; y < H; y++)
	{
		cin >> S[y];
	}
}

void daminput() {
}

template<class F>
class MaxFEdge {
public:
	int to;
	F cap;
	int rev;
	MaxFEdge(int _to, F _cap, int _rev) :to(_to), cap(_cap), rev(_rev) {}
};

template<class F>
class Dinic {
public:
	int V;
	vector<vector<MaxFEdge<F>>> G;
	VI itr, level;
	Dinic(int _v) :V(_v) {
		G.resize(V);
	}
	void AddEdge(int src, int to, F cap) {
		G[src].push_back(MaxFEdge<F>(to, cap, G[to].size()));
		G[to].push_back(MaxFEdge<F>(src, 0, G[src].size() - 1));
	}
	void bfs(int s) {
		level.assign(V, -1);
		queue<int> q;
		level[s] = 0;
		q.push(s);
		while (!q.empty()) {
			int v = q.front();
			q.pop();
			for (auto& e : G[v]) {
				if (e.cap > 0 && level[e.to] < 0) {
					level[e.to] = level[v] + 1;
					q.push(e.to);
				}
			}
		}
	}
	F dfs(int v, int t, F f) {
		if (v == t)return f;
		F res = 0;
		for (int& i = itr[v]; i < (int)G[v].size(); i++) {
			MaxFEdge<F>& e = G[v][i];
			if (e.cap > 0 && level[v] < level[e.to]) {
				F d = dfs(e.to, t, min(f, e.cap));
				if (d > 0) {
					e.cap -= d;
					G[e.to][e.rev].cap += d;
					res += d;
					f -= d;
				}
			}
		}
		return res;
	}
	F MaxFlow(int s, int t) {
		F res = 0;
		F f = 0;
		F inf = numeric_limits<F>::max();
		while (true) {
			bfs(s);
			if (level[t] < 0)return res;
			itr.assign(V, 0);
			while (true) {
				f = dfs(s, t, inf);
				if (f <= 0)break;
				res += f;
			}
		}
	}
};

int conv(int y, int x, bool isout) {
	int res = y * W + x;
	res *= 2;
	if (isout)return res + 1;
	else return res;
}

int vect[4][2] = {
	{1,0},{-1,0},{0,1},{0,-1}
};

void solve() {
	Dinic<int> D(2 * W * H + 2);
	for (int y = 0; y < H; y++)
	{
		for (int x = 0; x < W; x++)
		{
			if (S[y][x] != 'X') {
				D.AddEdge(conv(y, x, false), conv(y, x, true), 1);
			}
			else {
				D.AddEdge(conv(y, x, false), conv(y, x, true), (int)1e6);
			}
			for (int v = 0; v < 4; v++)
			{
				int nx = x + vect[v][1];
				int ny = y + vect[v][0];
				if (nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
				D.AddEdge(conv(y, x, true), conv(ny, nx, false), (int)1e6);
			}
			if (S[y][x] == 'X') {
				D.AddEdge(2 * W * H, conv(y, x, false), (int)1e6);
			}
			if (x == 0 || x == W - 1 || y == 0 || y == H - 1) {
				D.AddEdge(conv(y, x, true), 2*W*H+1,(int)1e6);
			}
		}
	}
	int maxf = D.MaxFlow(2 * W * H, 2 * W * H + 1);
	if (maxf >= (int)1e6)cout << "-1\n";
	else cout << maxf << "\n";
}

Measured memory_bytes: 5744