#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
template <typename T>
struct Dinic {
  struct Edge {
    int dst, rev;
    T cap;
    Edge(int dst, T cap, int rev) : dst(dst), cap(cap), rev(rev) {}
  };

  vector<vector<Edge> > graph;

  Dinic(int n) : graph(n), level(n), itr(n) {}

  void add_edge(int src, int dst, T cap) {
    graph[src].emplace_back(dst, cap, graph[dst].size());
    graph[dst].emplace_back(src, 0, graph[src].size() - 1);
  }

  T maximum_flow(int s, int t, T limit) {
    T res = 0;
    while (true) {
      bfs(s);
      if (level[t] == -1) return res;
      fill(ALL(itr), 0);
      T tmp;
      while ((tmp = dfs(s, t, limit)) > 0) res += tmp;
    }
  }

private:
  vector<int> level, itr;

  void bfs(int s) {
    fill(ALL(level), -1);
    queue<int> que;
    level[s] = 0;
    que.emplace(s);
    while (!que.empty()) {
      int ver = que.front(); que.pop();
      for (Edge e : graph[ver]) if (level[e.dst] == -1 && e.cap > 0) {
        level[e.dst] = level[ver] + 1;
        que.emplace(e.dst);
      }
    }
  }

  T dfs(int ver, int t, T flow) {
    if (ver == t) return flow;
    for (; itr[ver] < graph[ver].size(); ++itr[ver]) {
      Edge &e = graph[ver][itr[ver]];
      if (level[ver] < level[e.dst] && e.cap > 0) {
        T tmp = dfs(e.dst, t, min(flow, e.cap));
        if (tmp > 0) {
          e.cap -= tmp;
          graph[e.dst][e.rev].cap += tmp;
          return tmp;
        }
      }
    }
    return 0;
  }
};

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int h, w; cin >> h >> w;
  vector<string> s(h); REP(i, h) cin >> s[i];
  int n = h * w;
  Dinic<int> dinic(n * 2 + 2);
  REP(j, w) {
    if (s[0][j] == 'X' || s[h - 1][j] == 'X') {
      cout << -1 << '\n';
      return 0;
    }
    dinic.add_edge(j * 2 + 1, n * 2 + 1, INF);
    dinic.add_edge(((h - 1) * w + j) * 2 + 1, n * 2 + 1, INF);
  }
  FOR(i, 1, h - 1) {
    if (s[i][0] == 'X' || s[i][w - 1] == 'X') {
      cout << -1 << '\n';
      return 0;
    }
    dinic.add_edge((i * w) * 2 + 1, n * 2 + 1, INF);
    dinic.add_edge((i * w + w - 1) * 2 + 1, n * 2 + 1, INF);
  }
  REP(i, h) REP(j, w) {
    if (s[i][j] == '.') {
      dinic.add_edge((i * w + j) * 2, (i * w + j) * 2 + 1, 1);
    } else {
      dinic.add_edge((i * w + j) * 2, (i * w + j) * 2 + 1, INF);
      dinic.add_edge(n * 2, (i * w + j) * 2 + 1, INF);
    }
    REP(k, 4) {
      int y = i + dy[k], x = j + dx[k];
      if (0 <= y && y < h && 0 <= x && x < w) {
        dinic.add_edge((i * w + j) * 2 + 1, (y * w + x) * 2, INF);
      }
    }
  }
  cout << dinic.maximum_flow(n * 2, n * 2 + 1, INF) << '\n';
  return 0;
}


Measured memory_bytes: 2944