#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

#define inf 99999999

class Dinic {
  int MAX_V;
  int INF;
  struct edge{ int to, cap, rev, icap, flow; };

  vector< vector<edge> > G;
  vector<int> level; //sからの距離
  vector<int> iter; //どこまで調べたか

  void max_flow_bfs(int s){
    fill(level.begin(), level.end(), -1);
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while(!que.empty()){
      int v = que.front(); que.pop();
      for(int i=0; i<G[v].size(); i++){
        edge &e = G[v][i];
        if(e.cap>0 && level[e.to]<0){
          level[e.to] = level[v] + 1;
          que.push(e.to);
        }
      }
    }
  }
  int max_flow_dfs(int v, int t, int f){
    if(v==t) return f;
    for(int &i=iter[v]; i<G[v].size(); i++){
      edge &e = G[v][i];
      if(e.cap>0 && level[v]<level[e.to]){
        int d = max_flow_dfs(e.to, t, min(f, e.cap));
        if(d>0){
          e.cap -= d;
          G[e.to][e.rev].cap += d;
          e.flow += d;
          return d;
        }
      }
    }
    return 0;
  }

public:
  Dinic(int N):MAX_V(N),G(N),level(N),iter(N){
    INF = inf;
  }

  void add_edge(int from, int to, int cap){
    G[from].push_back((edge){to, cap, G[to].size(), cap, 0});
    G[to].push_back((edge){from, 0, G[from].size()-1, 0, 0});
  }

  int get_flow(int from, int to){ //untried
    rep(i,G[from].size()){
      if(G[from][i].to == to){
        return G[from][i].flow;
      }
    }
    return -1;
  }

  int max_flow(int s, int t){
    int flow = 0;
    while(true){
      max_flow_bfs(s);
      if(level[t]<0) return flow;
      fill(iter.begin(), iter.end(), 0);
      int f;
      while((f = max_flow_dfs(s, t, INF))>0){
        flow += f;
      }
    }
  }
  
  int min_cut(int s, int t, vector<int>& S, vector<int>& T){
    S.clear();
    T.clear();

    int maxf = max_flow(s, t);
    for(int i=0; i<level.size(); i++){
      if(level[i] >= 0) S.push_back(i);
      else T.push_back(i);
    }

    return maxf;
  }
};

int vx[4] = {1,-1,0,0};
int vy[4] = {0,0,1,-1};

int main(){
  int H, W;
  cin >> H >> W;
  vector<string> field;
  vector< vector<int> > num(H, vector<int>(W));
  rep(i,H){
    string s;
    cin >> s;
    field.push_back(s);
    rep(j,W){
      num[i][j] = i*W+j;
    }
  }

  Dinic dinic(2*H*W+2);
  int s = 2*H*W;
  int t = s+1;
  rep(i,H){
    rep(j,W){
      int in = i*W+j;
      int out = i*W+j + H*W;
      if(i==0 || i==H-1 || j==0 || j==W-1){
        dinic.add_edge(s,in,inf);
        if(field[i][j] == 'X'){
          cout << -1 << endl;
          return 0;
        }
      }

      if(field[i][j] == 'X'){
        dinic.add_edge(in,out,inf);
        dinic.add_edge(out,t,inf);
      }else{
        dinic.add_edge(in,out,1);
        rep(k,4){
          int nx = j + vx[k];
          int ny = i + vy[k];
          if(0<=nx&&nx<W && 0<=ny&&ny<H){
            dinic.add_edge(out,ny*W+nx,inf);
          }
        }
      }
    }
  }
  vector<int> S, T;
  int ret = dinic.min_cut(s,t,S,T);
  cout << ret << endl;
  
  
  return 0;
}


Measured memory_bytes: 3840