#include <bits/stdc++.h>
 
using namespace std;
 
#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fileio freopen("in.in", "r", stdin),freopen("out.out", "w", stdout);
#define ll long long int
#define FF first
#define SS second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define sd(x) scanf("%d",&x)
#define slld(x) scanf("%lld",&x)
#define pd(x) printf("%d\n",x)
#define plld(x) printf("%lld\n",x)
#define pss printf
#define MOD 1000000007
#define INF 1e6
#define eps 0.00001
#define endl '\n'
#define debug(n1) cout<<n1<<endl
ll h,w;
bool vis[105][105];
string s;
ll get(ll i,ll j)
{
	return (i-1)*w+j;
}
typedef long long LL;

struct Edge {
  int from, to, cap, flow, index;
  Edge(int from, int to, int cap, int flow, int index) :
    from(from), to(to), cap(cap), flow(flow), index(index) {}
};

struct PushRelabel {
  int N;
  vector<vector<Edge> > G;
  vector<LL> excess;
  vector<int> dist, active, count;
  queue<int> Q;

  PushRelabel(int N) : N(N), G(N), excess(N), dist(N), active(N), count(2*N) {}

  void AddEdge(int from, int to, int cap) {
    G[from].push_back(Edge(from, to, cap, 0, G[to].size()));
    if (from == to) G[from].back().index++;
    G[to].push_back(Edge(to, from, 0, 0, G[from].size() - 1));
  }

  void Enqueue(int v) { 
    if (!active[v] && excess[v] > 0) { active[v] = true; Q.push(v); } 
  }

  void Push(Edge &e) {
    int amt = int(min(excess[e.from], LL(e.cap - e.flow)));
    if (dist[e.from] <= dist[e.to] || amt == 0) return;
    e.flow += amt;
    G[e.to][e.index].flow -= amt;
    excess[e.to] += amt;    
    excess[e.from] -= amt;
    Enqueue(e.to);
  }
  
  void Gap(int k) {
    for (int v = 0; v < N; v++) {
      if (dist[v] < k) continue;
      count[dist[v]]--;
      dist[v] = max(dist[v], N+1);
      count[dist[v]]++;
      Enqueue(v);
    }
  }

  void Relabel(int v) {
    count[dist[v]]--;
    dist[v] = 2*N;
    for (int i = 0; i < G[v].size(); i++) 
      if (G[v][i].cap - G[v][i].flow > 0)
	dist[v] = min(dist[v], dist[G[v][i].to] + 1);
    count[dist[v]]++;
    Enqueue(v);
  }

  void Discharge(int v) {
    for (int i = 0; excess[v] > 0 && i < G[v].size(); i++) Push(G[v][i]);
    if (excess[v] > 0) {
      if (count[dist[v]] == 1) 
	Gap(dist[v]); 
      else
	Relabel(v);
    }
  }

  LL GetMaxFlow(int s, int t) {
    count[0] = N-1;
    count[N] = 1;
    dist[s] = N;
    active[s] = active[t] = true;
    for (int i = 0; i < G[s].size(); i++) {
      excess[s] += G[s][i].cap;
      Push(G[s][i]);
    }
    
    while (!Q.empty()) {
      int v = Q.front();
      Q.pop();
      active[v] = false;
      Discharge(v);
    }
    
    LL totflow = 0;
    for (int i = 0; i < G[s].size(); i++) totflow += G[s][i].flow;
    return totflow;
  }
};
int main()
{
	SPEED;
	memset(vis,1,sizeof vis);
	cin>>h>>w;
	for(int i=1;i<=h;i++)
	{
		cin>>s;
		for(int j=1;j<=w;j++)
		{
			if(s[j-1]!='X')
			{
				vis[i][j]=0;
			}
		}
	}
	for(int i=1;i<=h;i++)
		if(vis[i][1]||vis[i][w])
			return cout<<-1<<endl,0;
	for(int i=1;i<=w;i++)
		if(vis[1][i]||vis[h][i])
			return cout<<-1<<endl,0;
	PushRelabel pr(2*h*w+5);
	for(int i=1;i<=h;i++)
		for(int j=1;j<=w;j++)
			pr.AddEdge(get(i,j),get(i,j)+h*w,(vis[i][j]?INF:1));
	for(int i=1;i<=h;i++)
	{
		for(int j=1;j<=w;j++)
		{
			if(vis[i][j])
				pr.AddEdge(0,get(i,j),INF);
			if(!vis[i-1][j])
				pr.AddEdge(get(i,j)+h*w,get(i-1,j),INF);
			if(!vis[i+1][j])
				pr.AddEdge(get(i,j)+h*w,get(i+1,j),INF);
			if(!vis[i][j-1])
				pr.AddEdge(get(i,j)+h*w,get(i,j-1),INF);
			if(!vis[i][j+1])
				pr.AddEdge(get(i,j)+h*w,get(i,j+1),INF);
		}
	}
	for(int i=1;i<=h;i++)
	{
		pr.AddEdge(h*w+get(i,1),2*h*w+1,INF);
		pr.AddEdge(h*w+get(i,w),2*h*w+1,INF);
	}
	for(int i=1;i<=w;i++)
	{
		pr.AddEdge(h*w+get(1,i),2*h*w+1,INF);
		pr.AddEdge(h*w+get(h,i),2*h*w+1,INF);
	}
	cout<<pr.GetMaxFlow(0,2*h*w+1)<<endl;
	return 0;
}

Measured memory_bytes: 3840