#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
#ifdef DEBUG_ 
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL<<60;

struct dinic {
    struct edge{
        int to;
        ll cap;
        int rev;
        bool isrev;
    };

    vector<vector<edge>> G;
    vector<int> level, iter;

    void bfs(int s) {
        level.assign(G.size(), -1);
        queue<int> que;
        level[s] = 0;
        que.push(s);
        while(que.size()) {
            int v = que.front(); que.pop();
            for(auto i: G[v]) {
                if(i.cap > 0 && level[i.to] < 0) {
                    level[i.to] = level[v] + 1;
                    que.push(i.to);
                }
            }
        }
    }

    ll dfs(int v, const int t, ll f) {
        if(v == t) return f;
        for(int &i = iter[v]; i<(ll)G[v].size(); ++i) {
            edge &e = G[v][i];
            if(e.cap > 0 && level[v] < level[e.to]) {
                ll d = dfs(e.to, t, min(f, e.cap));
                if(d > 0) {
                    e.cap -= d;
                    G[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }

    dinic() {}
    dinic(int n) : G(n), level(n), iter(n) {}

    void add_edge(int from, int to, ll cap) {
        G[from].push_back({to, cap, (int)G[to].size(), false});
        G[to].push_back({from, 0, (int)G[from].size()-1, true});
    }

    ll max_flow(int s, int t) {
        ll flow = 0;
        while(1) {
            bfs(s);
            if(level[t] < 0) return flow;
            iter.assign(G.size(), 0);
            ll f;
            while((f = dfs(s, t, INF)) > 0) flow += f;
        }
    }

    friend ostream &operator <<(ostream& out, const dinic& a){
        out << endl;
        for(int i = 0; i < (int)a.G.size(); i++) {
            for(auto &e : a.G[i]) {
                if(e.isrev) continue;
                auto &rev_e = a.G[e.to][e.rev];
                if(rev_e.cap == 0) continue;
                out << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
            }
        }
        return out;
    }
};

int main(void) {
    ll h, w;
    cin >> h >> w;
    vector<string> s(h);
    REP(i, h) cin >> s[i];

    REP(y, h) {
        if(s[y][0]=='X' || s[y][w-1]=='X') {
            cout << -1 << endl;
            return 0;
        }
    }
    REP(x, w) {
        if(s[0][x]=='X' || s[h-1][x]=='X') {
            cout << -1 << endl;
            return 0;
        }
    }

    dinic flow(2*h*w+2);
    ll S = 2*h*w, T = 2*h*w+1;

    ll dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
    REP(x, w) REP(y, h) {
        if(s[y][x]=='X') flow.add_edge((x*h+y)*2, (x*h+y)*2+1, INF);
        else flow.add_edge((x*h+y)*2, (x*h+y)*2+1, 1);
        REP(i, 4) {
            ll nx = x + dx[i], ny = y + dy[i];
            if(nx<0 || nx>=w || ny<0 || ny>=h) continue;
            ll va = (x*h + y)*2 + 1;
            ll vb = (nx*h + ny)*2;
            flow.add_edge(va, vb, INF);
        }
    }
    REP(y, h) REP(x, w) {
        if(s[y][x]=='.') continue;
        ll va = (x*h + y)*2;
        flow.add_edge(S, va, INF);
    }
    REP(x, w) {
        ll v = (x*h)*2+1;
        flow.add_edge(v, T, INF);
        v = (x*h + h-1)*2+1;
        flow.add_edge(v, T, INF);
    }
    REP(y, h) {
        ll v = (0*h + y)*2+1;
        flow.add_edge(v, T, INF);
        v = ((w-1)*h + y)*2+1;
        flow.add_edge(v, T, INF);
    }

    cout << flow.max_flow(S, T) << endl;
    dump(flow);

    return 0;
}

Measured memory_bytes: 4736