#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))

// mod
const ll MOD = 1000000007ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

// floating
typedef double Real;
const Real EPS = 1e-11;
#define EQ0(x) (abs(x)<EPS)
#define EQ(a,b) (abs(a-b)<EPS)
typedef complex<Real> P;

const int INF = 830252521;
typedef int flow_type;
// Dinic http://www.prefield.com/algorithm/graph/dinic.html
struct dinic{
  struct edge{int from,to;flow_type cost;int rev;};
  int n;
  vector< vector<edge> > G;
  vector< vector<edge> > C;
  // vector< vector<flow_type> > flow, capacity;
// #define RESIDUE(s,t) (capacity[s][t]-flow[s][t])
  vi level;
  vector<bool> finished;
  dinic(int _n){
    n = _n;
    G.assign(n,vector<edge>());
    // flow.assign(n,vector<flow_type>(n,0));
    // capacity.assign(n,vector<flow_type>(n,0));
    level.assign(n,0);
    finished.assign(n,false);
  }
  void add_edge(int from,int to,flow_type cost){
    assert(0<=from && from<n);
    assert(0<=to && to<n);
    edge s = (edge){from,to,cost,(int)G[to].size()};
    edge t = (edge){to,from,0,(int)G[from].size()};
    G[from].push_back(s);
    G[to].push_back(t);
  }
  flow_type dfs(int u, int t, flow_type cur){
    if(u==t || cur==0) return cur;
    if(finished[u]) return 0;
    finished[u] = true;
    REP(i,C[u].size()){
      edge &e = C[u][i];
      if(level[e.to] > level[u]){
        flow_type f = dfs(e.to, t, min(cur, e.cost));
        if(f>0){
          e.cost -= f;
          C[e.to][e.rev].cost += f;
          finished[u] = false;
          return f;
        }
      }
    }
    return 0;
  }
  flow_type calc(int s, int t){
    C = G;
    flow_type total = 0;
    while(true){
      REP(i,n)level[i] = -1;
      level[s] = 0;
      queue<int> Q; Q.push(s);
      int d = n;
      while(!Q.empty()){
        int u = Q.front(); Q.pop();
        REP(i,C[u].size()){
          edge &e = C[u][i];
          if(e.cost > 0 && level[e.to] == -1){
            Q.push(e.to);
            level[e.to] = level[u] + 1;
          }
        }
      }
      REP(i,n)finished[i]=false;
      bool flag = false;
      while(true){
        flow_type f = dfs(s,t,INF);
        if(f==0)break;
        total += f;
        flag = true;
      }
      if(!flag)break;
    }
    return total;
  }
};

int h,w;
char mp[125][125];
int dd[] = {1,0,-1,0,1};

int main(){
  scanf("%d%d",&h,&w);
  REP(i,h)scanf("%s",mp[i]);
  // flow
  dinic g(2*h*w+2);
  int s=2*h*w;
  int t=2*h*w+1;
  REP(i,h)REP(j,w){
    int id = i*w + j;
    int x=2*id,y=2*id+1;
    if(mp[i][j]=='X'){
      if(i==0||i==h-1||j==0||j==w-1){
        puts("-1");
        return 0;
      }
      g.add_edge(s,x,INF);
      g.add_edge(x,y,INF);
    }else{
      g.add_edge(x,y,1);
    }
    if(i==0||i==h-1||j==0||j==w-1){
      g.add_edge(y,t,INF);
    }
    REP(d,4){
      int ni=i+dd[d],nj=j+dd[d+1];
      if(ni<0||ni>=h||nj<0||nj>=w)continue;
      int nid = ni*w + nj;
      g.add_edge(y,2*nid,INF);
    }
  }
  printf("%d\n",g.calc(s,t));
  return 0;
}


Measured memory_bytes: 5760