#include <bits/stdc++.h>
// #include "ane.cpp"

const int INF  = 1e9;
const long long INFLL = 1e18;
const int NMAX = 105;
const int MMAX = 105;
const int KMAX = 1005;
const int MOD  = 1e9 + 7;
using namespace std;

// comment to disable debug functions
#define DEBUG

// frequently used macros

#if __cplusplus >= 201103L
#define ALL(v) begin(v),end(v)
#define SORT(v) sort(begin(v), end(v))
#define FIND(v,x) find(begin(v), end(v), (x))
#else
#define ALL(v) (v).begin(),(v).end()
#define SORT(v) sort(v.begin(), v.end())
#define FIND(v,x) find(v.begin(), v.end(), (x))
#endif

#define fi first
#define se second

#define MEMNEXT(from, to) do{ memmove((to), (from), sizeof(from)); \
memset((from), 0, sizeof(from)); } while(0)
#ifdef DEBUG
#define DUMP(x) do{ std::cerr << (#x) << ": " << x << std::endl; }while(0)
#else
#define DUMP(x) do{}while(0)
#endif

// frequent used aliases
typedef long long ll;
typedef pair<int, int> p;
typedef pair<ll, int> lp;
typedef pair<ll, ll> llp;
typedef vector<int> vec;
typedef vector<ll> vecll;
typedef vector<vec> mat;
typedef vector<vecll> matll;

// frequently used constants
static const int di[] = {-1, 0, 1, -1, 1, -1, 0, 1};
static const int dj[] = {-1, -1, -1, 0, 0, 1, 1, 1};

// frequently used structs
struct edge{
  int to,cost;
};

// printf for debug
#ifndef DEBUG
void debug(const char* format, ...){}
#else
void debug(const char* format, ...){
  va_list arg;
  va_start(arg, format);
  vprintf(format, arg);
  va_end(arg);
}
#endif

// dump vector
#ifdef DEBUG
#define DUMPV(v, c) do{       \
  printf("%s: ", #v);         \
  for (int i = 0; i < (c); ++i) \
  {                           \
  cout << (v)[i] << " ";      \
  }                           \
  cout << endl;               \
} while(0)
#else
#define DUMPV(v,c)
#endif

// std::fill of multi dimensions
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
  std::fill( (T*)array, (T*)(array+N), val );
}

// binary search
ll BSearch(ll _begin, ll _end, bool (*f)(ll)){
  ll mid;
  while(_end - _begin > 1LL) {
  mid = _begin + (_end - _begin) / 2LL;
  if(f(mid)) {
    debug("BSearch: f(%d) == true\n", mid);
    _end = mid;
  }
  else
  {
    debug("BSearch: f(%d) == false\n", mid);
    _begin = mid;
  }
  }
  return _end;
}
template<typename T>
  class MaxFlow{
    struct mf_edge{int to, rev; T cap;};
    vector<vector<mf_edge> > G;
    bool isNondir, initialized;
    vector<bool> used;
    T MAX;

    T dfs_ff(int i, int t, T f){

      if(i == t) return f;
      used[i] = true;
      for(auto&& e : G[i]) {
        T ff = 0;
        if (!used[e.to] && e.cap > 0 && (ff = dfs_ff(e.to, t, min<T>(f, e.cap)) > 0))
        {
          e.cap -= ff;
          G[e.to][e.rev].cap += ff;
          return ff;
        }
      }
      return 0;
    }
  public:
    MaxFlow(){}
    void init(int n, T _max, bool nondir = false){
      G.assign(n, vector<mf_edge>());
      isNondir = nondir;
      MAX = _max;
      initialized = true;
    }
    void add_edge(int i, int j, T cap){
      assert(i < G.size());
      assert(j < G.size());
      G[i].push_back(mf_edge{j, (int)G[j].size(), cap});
      G[j].push_back(mf_edge{i, (int)G[i].size() - 1, 0});
      if(isNondir){
        G[j].push_back(mf_edge{i, (int)G[i].size(), cap});
        G[i].push_back(mf_edge{j, (int)G[j].size() - 1, 0});
      }
    }
    T ford_fulkerson(int s, int t){
      assert(s < G.size());
      assert(t < G.size());
      if (!initialized)
      {
        cerr << "error: MaxFlow not initialized." << endl;
        return 0;
      }
      T ret = 0;
      while(true){
        used.assign(G.size(), false);
        T d = dfs_ff(s, t, MAX);
        if(d == 0)return ret;
        ret += d;
      }
    }
  };

ll NUM_TEST_CASE = 1;

ll N,M,K,A,B,C,D,E;
char dat[NMAX][MMAX];
MaxFlow<ll> mf;
ll ans = {};

void solve(){
  // main algorithm
  #define IN(i, j) ((i) * M + (j))
  #define OUT(i, j) (N * M + (i) * M + (j))
  #define START (2 * N * M)
  #define GOAL (2 * N * M + 1)

  mf.init(2 * N * M + 2, INFLL);
  for (int i = 0; i < N; ++i)
  {
    for (int j = 0; j < M; ++j)
    {
      mf.add_edge(IN(i, j), OUT(i, j), 1);
      if (i == 0 || i == N-1 || j == 0 || j == M-1)
      {
        if(dat[i][j] == 'X'){
          ans = -1;
          return;
        }
        mf.add_edge(OUT(i, j), GOAL, INFLL);
      }
      if(dat[i][j] == 'X') mf.add_edge(START, OUT(i, j), INFLL);
      if(i > 0)     mf.add_edge(OUT(i, j), IN(i-1, j), INFLL);
      if(i < N - 1) mf.add_edge(OUT(i, j), IN(i+1, j), INFLL);
      if(j > 0)     mf.add_edge(OUT(i, j), IN(i, j-1), INFLL);
      if(j < M - 1) mf.add_edge(OUT(i, j), IN(i, j+1), INFLL);
    }
  }
  ans = mf.ford_fulkerson(START, GOAL);

  #undef IN
  #undef OUT
  #undef START
  #undef GOAL
}
void debug(){
  // output debug information

}
void answer(){
  // output answer
  cout << ans << endl;
}
void init(){
  // initialize for each test case
  // Fill(dp, -1);
}
int main(int argc, char const *argv[])
{
  // operate inputs
  // cin >> NUM_TEST_CASE;

  for (int test_case = 0; test_case < NUM_TEST_CASE; ++test_case)
  {
    init();

    cin >> N >> M;
    for (int i = 0; i < N; ++i)
    {
      for (int j = 0; j < M; ++j)
      {
        scanf(" %c", &dat[i][j]);
      }
    }
    solve();
    #ifdef DEBUG
    debug();
    #endif
    answer();
  }

  return 0;
}

Measured memory_bytes: 3328