using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		for(int i=0;i<H;i++){
			if(S[i][0] == 'X' || S[i][W-1] == 'X' ){
				Console.WriteLine(-1);
				return;
			}
		}
		for(int j=0;j<W;j++){
			if(S[0][j] == 'X' || S[H-1][j] == 'X' ){
				Console.WriteLine(-1);
				return;
			}
		}
		
		var f = new MaxFlow_Dinic();
		int nnode = H*W*2;
		f.Init(nnode + 2);
		int s = H*W*2;
		int t = s + 1;
		Func<int,int,int,int> enc = (r,c,d) => {
			return d + 2*c + 2*W*r;
		};
		
		Func<int,int,bool> InRange = (r,c) => {
			return 0 <= r && r < H && 0 <= c && c < W;
		};
		
		int[] dx = new int[] {1,0,-1,0};
		int[] dy = new int[] {0,-1,0,1};
		
		var Inf = (int)1e9;
		
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				if(S[i][j] == 'X'){
					f.AddEdge(enc(i,j,0), enc(i,j,1), Inf);
					f.AddEdge(s, enc(i,j,0), Inf);
				}else{
					f.AddEdge(enc(i,j,0), enc(i,j,1), 1);
				}
				
				for(int tt=0;tt<4;tt++){
					int ny = i + dy[tt];
					int nx = j + dx[tt];
					if(!InRange(ny,nx))continue;
					f.AddEdge(enc(i,j,1), enc(ny,nx,0), Inf);
				}
				if(i == 0 || i == H-1 || j == 0 || j == W-1){
					f.AddEdge(enc(i,j,1), t, Inf);
				}
			}
		}
		
		Console.WriteLine(f.MaxFlow(s,t));
		
		
		
		
		
		
	}
	int H,W;
	String[] S;
	public Sol(){
		var d = ria();
		H = d[0]; W = d[1];
		S = new String[H];
		for(int i=0;i<H;i++){
			S[i] = rs();
		}
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}


class MaxFlow_Dinic{
	// arihon is god
	class Edge {
		public int To,Cap,Rev;
		public Edge(int to = 0,int cap = 0,int rev = 0){
			To = to;
			Cap = cap;
			Rev = rev;
		}
	}
	
	List<Edge>[] G;
	int[] Level;
	int[] Iter;
	
	static int Inf = (int) 1e9;
	public int N;
	public void Init(int n){
		N = n;
		G = new List<Edge>[N];
		for(int i=0;i<N;i++) G[i] = new List<Edge>();
		Level = new int[N];
		Iter = new int[N];
	}
	
	
	public void AddEdge(int from, int to, int cap){
		G[from].Add(new Edge(to,cap,G[to].Count));
		G[to].Add(new Edge(from,0,G[from].Count-1));
	}
	
	void bfs(int s){
		for(int j=0;j<G.Length;j++)Level[j] = -1;
		var Q = new Queue<int>();
		Level[s] = 0;
		Q.Enqueue(s);
		
		while(Q.Count>0){
			var v = Q.Dequeue();
			foreach(var e in G[v]){
				if(e.Cap > 0 && Level[e.To] < 0){
					Level[e.To] = Level[v] + 1;
					Q.Enqueue(e.To);
				}
			}
		}
	}
	
	int dfs(int v, int t, int f){
		if(v == t) return f;
		for(;Iter[v] < G[v].Count; Iter[v]++){
			var e = G[v][Iter[v]];
			if(e.Cap > 0 && Level[v] < Level[e.To]){
				int d = dfs(e.To, t, Math.Min(f,e.Cap));
				if(d > 0){
					e.Cap -= d;
					G[e.To][e.Rev].Cap += d;
					return d;
				}
			}
		}
		return 0;
	}
	
	public int MaxFlow(int s, int t){
		int flow = 0;
		while(true){
			bfs(s);
			if(Level[t] < 0) return flow;
			Iter = new int[N];
			int f;
			while( ( f = dfs(s,t,Inf)) > 0){
				flow += f;
			}
		}
	}
	
	
}


Measured memory_bytes: 14808