#include <iostream>
#include <algorithm>
#include <climits>
#include <queue>
#include <cstring>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

#include <cstdio>
inline int getInt(){ int s; scanf("%d", &s); return s; }

#include <set>

using namespace std;

char g[128][128];

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))

#include <vector>

struct Edge{
  int cap; // capacity
  int to;
  int rev; // reverse edge id

  Edge(){}
  Edge(int c, int t, int r) :
    cap(c), to(t), rev(r){}
};

template<class E> // Edge type
class Graph{
public:
  typedef std::vector<std::vector<E> > G;

private:
  G g;

public:
  Graph(int n) : g(G(n)) {}

  void addEdge(int from, int to, int cap){
    g[from].push_back(E(cap, to, g[to].size()));
    g[to].push_back(E(0, from, g[from].size() - 1));
  }

  void addEdge(int from, int to, int cap, int cost){
    g[from].push_back(E(cap, to, cost, g[to].size()));
    g[to].push_back(E(0, from, -cost, g[from].size() - 1));
  }

  G &getRowGraph(){
    return g;
  }
};

template<class E>
class Dinic{
  typedef typename Graph<E>::G G;
  G &g;
  std::size_t n; // size of graph

  std::vector<int> level;
  std::vector<int> iter;

  // other utilities

  // search length of shortest path from s
  void bfs(int s){
    std::queue<int> que;
    level = std::vector<int>(n, -1);

    level[s] = 0;
    que.push(s);

    while(!que.empty()){
      int v = que.front(); que.pop();
      for(int i = 0; i < (int)g[v].size(); i++){
        E &e = g[v][i];
        if(e.cap > 0 && level[e.to] < 0){
          level[e.to] = level[v] + 1;
          que.push(e.to);
        }
      }
    }
  }

  // search path
  int dfs(int v, int t, int f){
    if(v == t) return f;
    for(int &i = iter[v]; i < (int)g[v].size(); i++){
      E &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;
  }

public:
  Dinic(Graph<E> &graph) : g(graph.getRowGraph()){
    n = g.size();
  }

  // Max flow of the flow from s to t.
  int solve(int s, int t){
    int flow = 0;
    while(true){
      int f;
      bfs(s);
      if(level[t] < 0) return flow;
      iter  = std::vector<int>(n, 0);
      while((f = dfs(s, t, INT_MAX)) > 0){
        flow += f;
      }
    }
  }
};

template<class E>
int dinic(Graph<E> &g, int s, int d){
  return Dinic<E>(g).solve(s, d);
}

int main(){
  const int h = getInt();
  const int w = getInt();
  REP(i,h) scanf("%s", g[i]);

  bool ok = true;
  REP(i,h) REP(j,w) if(g[i][j] == 'X'){
    if(i == 0 || i == h - 1 || j == 0 || j == w - 1) ok = false;
  }

  if(!ok) {
    puts("-1");
    return 0;
  }

  const int inf = 10000;
  Graph <Edge> g(2 * h * w + 2);
  const int src = 2 * h * w;
  const int dst = src + 1;

  REP(i,h) REP(j,w) {
    const int p = 2 * (i * w + j);

    g.addEdge(p, p + 1, 1);
    REP(k,4) {
      const int x = j + _dx[k];
      const int y = i + _dy[k];

      if(ISIN(x, y, w, h)) {
        const int pp = 2 * (y * w + x);
        g.addEdge(p + 1, pp, inf);
      } else {
        g.addEdge(p + 1, dst, inf);
      }
    }

    if(::g[i][j] == 'X'){
      g.addEdge(src, p + 1, inf);
    }
  }

  const int ans = dinic(g, src, dst);
  printf("%d\n", ans);

  return 0;
}


Measured memory_bytes: 2944