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

typedef long long   signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define  inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define  dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))

#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define PQ priority_queue

#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)
#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)

template<typename T> bool   setmin(T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }
template<typename T> bool   setmax(T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

template<typename C> struct Flow {
	int V;
	C INF;
	struct E { int t, r; C c; };
	vector<vector<E>> g;
	vector<bool> used;
	
	Flow() { }
	Flow(int v, C inf) { init(v, inf); }
	void init(int v, C inf) {
		V = v;
		INF = inf;
		g.resize(V);
		used.resize(V);
	}
	
	void add(int f, int t, C c) {
		assert(inID(f, 0, V));
		assert(inID(t, 0, V));
		int gt = g[t].size();
		int gf = g[f].size();
		g[f].PB({ t, gt, c });
		g[t].PB({ f, gf, 0 });
	}
	
	C dfs(int f, int t, C c) {
		if(f == t) { return c; }
		used[f] = true;
		inc(i, g[f].size()) {
			E & e = g[f][i];
			if(used[e.t] || e.c <= 0) { continue; }
			C v = dfs(e.t, t, min(c, e.c));
			if(v > 0) {
				e.c -= v;
				g[e.t][e.r].c += v;
				return v;
			}
		}
		return 0;
	}
	
	C max_flow(int S, int T) {
		assert(inID(S, 0, V));
		assert(inID(T, 0, V));
		C ans = 0;
		while(true) {
			inc(i, V) { used[i] = false; }
			C f = dfs(S, T, INF);
			ans += f;
			if(f == 0) { break; }
		}
		return ans;
	}
};

// ----

int h, w, S, T;
string s[100];

int id_i(int i, int j) { return (inID(i, 0, h) && inID(j, 0, w) ? w * i + j : T); }
int id_o(int i, int j) { return (h * w) + w * i + j; }

int main() {
	cin >> h >> w;
	inc(i, h) { cin >> s[i]; }
	
	Flow<int> fl(2 * h * w + 2, 10000);
	S = 2 * h * w;
	T = S + 1;
	
	inc(i, h) {
	inc(j, w) {
		if(s[i][j] == 'X') { fl.add(S, id_o(i, j), fl.INF); }
		fl.add(id_i(i, j), id_o(i, j), 1);
		fl.add(id_o(i, j), id_i(i + 1, j), fl.INF);
		fl.add(id_o(i, j), id_i(i - 1, j), fl.INF);
		fl.add(id_o(i, j), id_i(i, j + 1), fl.INF);
		fl.add(id_o(i, j), id_i(i, j - 1), fl.INF);
	}
	}
	
	int ans = fl.max_flow(S, T);
	if(ans >= fl.INF) { ans = -1; }
	
	cout << ans << endl;
	
	return 0;
}


Measured memory_bytes: 3072