#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <random>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;

typedef
tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> orderedSet;

#define pb push_back
#define F first
#define S second
#define all(a) (a).begin(), (a).end()

#define for1(i0, l0, r0) for (int i0 = l0; i0 < r0; ++i0)
#define for2(i0, l0, r0) for (int i0 = l0; i0 <= r0; ++i0)
#define forn(i0, n0) for (int i0 = 0; i0 < n0; ++i0)
#define forn1(i0, n0) for (int i0 = 1; i0 < n0; ++i0)
#define forr(i0, n0) for (int i0 = n0; i0 >= 0; --i0)
#define forr1(i0, r0, l0) for (int i0 = r0; i0 > l0; --i0)
#define forr2(i0, r0, l0) for (int i0 = r0; i0 >= l0; --i0)

#define Sort(a) sort(all(a))
#define Reverse(a) reverse(all(a))

typedef long double ld;
#define ui unsigned int
#define ull unsigned long long

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef pair<string, string> pss;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;

typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<pdd> vpdd;

typedef vector<vi> vvi;
typedef vector<vd> vvd;
typedef vector<vll> vvll;
typedef vector<vb> vvb;

#define vpss vector<pss>
#define vvs vector<vs>
#define vvpii vector<vpii>
#define vvpll vector<vpll>
#define vpt vector<pt>
#define vvvi vector<vvi>
#define vsi vector<set<int>>
#define ss second
#define ff first

#define printvi(arr) for (int x0 : arr) cout << x0 << ' '; cout << '\n';
#define printvll(arr) for (ll x0 : arr) cout << x0 << ' '; cout << '\n';
#define printpair(pair0) cout << pair0.F << ' ' << pair0.S << '\n';
#define printvp(arr) for (auto pair0 : arr) printpair(pair0);
#define initArray(arr, N0, X0) memset(arr, X0, N0 * sizeof(int))
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void init() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //cout.precision(16);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
}

const int inf = 1e9;
const int maxn = 20005;
const int N = 10000;

struct edge {
    int t, c, r;
};

vector<edge> G[maxn];
bool used[maxn];

void add_edge(int v, int u, int c = 1) {
    G[v].push_back((edge) {u, c, (int) G[u].size()});
    G[u].push_back((edge) {v, 0, (int) G[v].size() - 1});
}

void connect(int v, int u, int c = 1) {
    add_edge(v, u + N, c);
    add_edge(u, v + N, c);
}

int dfs(int v, int t, int f) {
    if (v == t) return f;
    used[v] = true;
    for (auto &it: G[v])
        if (!used[it.t] && it.c) {
            int d = dfs(it.t, t, min(f, it.c));
            if (d) {
                it.c -= d;
                G[it.t][it.r].c += d;
                return d;
            }
        }
    return 0;
}

int maxFlow(int s, int t) {
    int res = 0, f = 1;
    while (f) {
        fill(used, used + maxn, 0);
        f = dfs(s, t, inf);
        res += f;
    }
    return res;
}

void solve() {
    int n, m;
    cin >> n >> m;
    vs a(n);
    forn(i, n) cin >> a[i];
    forn(i, n) {
        forn(j, m) {
            if (i == 0 || j == 0 || i == n - 1 || j == m - 1) {
                if (a[i][j] == 'X') {
                    cout << "-1\n";
                    return;
                }
            }
        }
    }
    int I = 2 * N, J = I + 1;
    vi dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
    forn(i, n) {
        forn(j, m) {
            add_edge(i * m + j + N, i * m + j, (a[i][j] == 'X' ? inf : 1));
            forn(k, 4){
                int i1 = i + dx[k], j1 = j + dy[k];
                if (i1 >= 0 && j1 >= 0 && i < n && j < m) connect(i * m + j, i1 * m + j1, inf);
            }
            if (a[i][j] == 'X') add_edge(I, i * m + j + N, inf);
            if (i == 0 || j == 0 || i + 1 == n || j + 1 == m) add_edge(i * m + j, J, inf);
        }
    }
    cout << maxFlow(I, J) << '\n';
}

bool multitest = false;

int main() {
    init();
    int t = 1;
    if (multitest) cin >> t;
    forn(i, t) solve();
}

Measured memory_bytes: 9088