#include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(a)-1;i>=b;i--)
#define chmin(a,b) (a)=min((a),(b));
#define chmax(a,b) (a)=max((a),(b));
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(v) cout<<(#v)<<":";for(auto(x):(v)){cout<<" "<<(x);}cout<<endl;
#define printVS(vs) cout<<(#vs)<<":"<<endl;for(auto(s):(vs)){cout<<(s)<< endl;}
#define printVV(vv) cout<<(#vv)<<":"<<endl;for(auto(v):(vv)){for(auto(x):(v)){cout<<" "<<(x);}cout<<endl;}
#define printP(p) cout<<(#p)<<(p).first<<" "<<(p).second<<endl;
#define printVP(vp) cout<<(#vp)<<":"<<endl;for(auto(p):(vp)){cout<<(p).first<<" "<<(p).second<<endl;}

inline void output(){ cout << endl; }
template<typename First, typename... Rest>
inline void output(const First& first, const Rest&... rest) {
    cout << first << " "; output(rest...);
}

using ll = long long;
using Pii = pair<int, int>;
using TUPLE = tuple<int, int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const int inf = 2e9;
const int mod = 1e9 + 7;
using Graph = vector<vector<int>>;

struct edge { int to, cap, rev; };

const int MAX_V = 20010;
vector<edge> G[MAX_V];
int level[MAX_V];
int iter[MAX_V];

void addEdge(int from, int to, int cap) {
    G[from].emplace_back((edge){to, cap, (int)G[to].size()});
    G[to].emplace_back((edge){from, 0, (int)G[from].size() - 1});
}

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

int dfs(int v, int t, int f) {
    if (v == t) return f;
    for (int &i = iter[v]; i < G[v].size(); i++) {
        edge &e = G[v][i];
        if (e.cap > 0 && level[v] < level[e.to]) {
            int 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;
}

int max_flow(int s, int t) {
    int flow = 0;
    for (;;) {
        bfs(s);
        if (level[t] < 0) return flow;
        memset(iter, 0, sizeof(iter));
        int f;
        while ((f = dfs(s, t, inf)) > 0) {
            flow += f;
        }
    }
}

#define dame cout << -1 << endl; return 0;

// N, E, S, W
const int dx[4] = {-1, 0,  1,  0};
const int dy[4] = { 0, 1,  0, -1};

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int n, m;
    cin >> n >> m;
    vector<string> s(n);
    rep(i, n) cin >> s[i];

    // rep(i, n) {
    //     if (s[i][0] == 'X' || s[i].back() == 'X') {
    //         dame;
    //     }
    // }
    //
    // rep(j, m) {
    //     if (s[0][j] == 'X' || s.back()[j] == 'X') {
    //         dame;
    //     }
    // }

    enum {IN, OUT};
    auto toNode = [&](int i, int j, int in) {
        return i * m + j + (in ? 0 : n * m);
    };

    auto inside = [&](int i, int j) {
        return 0 <= i && i < n && 0 <= j && j < m;
    };

    const int src = 2 * n * m;
    const int fin = 2 * n * m + 1;
    rep(i, n) {
        rep(j, m) {
            if (s[i][j] == 'X') {
                addEdge(src, toNode(i, j, OUT), inf);
            }
            addEdge(toNode(i, j, IN), toNode(i, j, OUT), (s[i][j] == 'X' ? inf : 1));

            rep(k, 4) {
                int ni = i + dx[k], nj = j + dy[k];
                if (inside(ni, nj)) {
                    addEdge(toNode(i, j, OUT), toNode(ni, nj, IN), inf);
                }
                else {
                    addEdge(toNode(i, j, OUT), fin, inf);
                }
            }
        }
    }

    int ans = max_flow(src, fin);
    cout << (ans < inf ? ans : -1) << endl;

}


Measured memory_bytes: 5436