#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <memory.h>
#include <string>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <queue>

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;

#define MP make_pair
#define PB push_back
#define FF first
#define SS second

#define FORN(i, n) for (int i = 0; i <  (int)(n); i++)
#define FOR1(i, n) for (int i = 1; i <= (int)(n); i++)
#define FORD(i, n) for (int i = (int)(n) - 1; i >= 0; i--)

#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR0(A,n) { cout << #A << " = "; FORN(_,n) cout << A[_] << ' '; cout << endl; }

#define MOD 1000000007
#define INF 2000000000

int GLL(LL& x) {
    return scanf("%lld", &x);
}

int GI(int& x) {
    return scanf("%d", &x);
}

struct Edge {
  int u, v;
  LL cap, flow;
  Edge() {}
  Edge(int u, int v, LL cap): u(u), v(v), cap(cap), flow(0) {}
};

struct Dinic {
  int N;
  vector<Edge> E;
  vector<vector<int>> g;
  vector<int> d, pt;
  
  Dinic(int N): N(N), E(0), g(N), d(N), pt(N) {}

  void AddEdge(int u, int v, LL cap) {
//    printf("ADD %d %d\n", u, v);
    if (u != v) {
      E.emplace_back(Edge(u, v, cap));
      g[u].emplace_back(E.size() - 1);
      E.emplace_back(Edge(v, u, 0));
      g[v].emplace_back(E.size() - 1);
    }
  }

  bool BFS(int S, int T) {
    queue<int> q({S});
    fill(d.begin(), d.end(), N + 1);
    d[S] = 0;
    while(!q.empty()) {
      int u = q.front(); q.pop();
      if (u == T) break;
      for (int k: g[u]) {
        Edge &e = E[k];
        if (e.flow < e.cap && d[e.v] > d[e.u] + 1) {
          d[e.v] = d[e.u] + 1;
          q.emplace(e.v);
        }
      }
    }
    return d[T] != N + 1;
  }

  LL DFS(int u, int T, LL flow = -1) {
    if (u == T || flow == 0) return flow;
    for (int &i = pt[u]; i < g[u].size(); ++i) {
      Edge &e = E[g[u][i]];
      Edge &oe = E[g[u][i]^1];
      if (d[e.v] == d[e.u] + 1) {
        LL amt = e.cap - e.flow;
        if (flow != -1 && amt > flow) amt = flow;
        if (LL pushed = DFS(e.v, T, amt)) {
          e.flow += pushed;
          oe.flow -= pushed;
          return pushed;
        }
      }
    }
    return 0;
  }

  LL MaxFlow(int S, int T) {
    LL total = 0;
    while (BFS(S, T)) {
      fill(pt.begin(), pt.end(), 0);
      while (LL flow = DFS(S, T))
        total += flow;
    }
    return total;
  }
};

int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

int h, w;

vector<string> board; string row;

inline int id(int x, int y, bool in) {
    return 2 * (x * w + y) + in + 2;
}

int main() {
    GI(h); GI(w);

    FORN(i, h) {
        cin >> row; board.PB(row);
    }

    Dinic mf(h * w * 2 + 5);

    FORN(i, h) {
        if (board[i][0] == 'X' || board[i][w-1] == 'X') {
            printf("-1\n");
            return 0;
        }
        else {
            mf.AddEdge(0, id(i, 0, true), INF);
            mf.AddEdge(0, id(i, w-1, true), INF);
        }
    }

    for (int j = 1; j < w-1; j++) {
        if (board[0][j] == 'X' || board[h-1][j] == 'X') {
            printf("-1\n");
            return 0;
        }
        else {
            mf.AddEdge(0, id(0, j, true), INF);
            mf.AddEdge(0, id(h-1, j, true), INF);
        }
    }

    FORN(i, h) {
        FORN(j, w) {
            if (board[i][j] == '.') {
                mf.AddEdge(id(i, j, true), id(i, j, false), 1);

                FORN(k, 4) {
                    int ni = i + dx[k];
                    int nj = j + dy[k];

                    if (ni >= 0 && ni < h && nj >= 0 && nj < w) {
                        mf.AddEdge(id(i, j, false), id(ni, nj, true), INF);
                    }
                }
            }
            else {
                mf.AddEdge(id(i, j, true), 1, INF);
            }
        }
    }

    printf("%lld\n", mf.MaxFlow(0, 1));
    return 0;
}


Measured memory_bytes: 4512