#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mem(x,n) memset(x,n,sizeof(x))
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define foreach(it,l) for(auto it=(l).begin();it!=(l).end();it++)
#define pnl printf("\n")
#define len(x) (x).length()

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
const double EPS=1e-10;
//const double PI=acos(-1.0);
const int INF=1<<30;
const LL LLINF=1ll<<60;
const int MOD=1e9+7;
const int xx[8]={1,0,-1,0,1,-1,1,-1};
const int yy[8]={0,1,0,-1,-1,1,1,-1};

/****************************************************************************************\
  __    __               __ /\/|   ___ _           _       /\/|__               __    __
  \ \  / /  _____ __ ____\ \/\/   / __| |__ _ _ __(_)___  |/\// /____ __ _____  \ \  / /
   \ \| |  |___\ V  V /___| |    | (__| / _` | '_ \ / -_)    | |___\ V  V /___|  | |/ /
    \_\ |  |___|\_/\_/|___| |     \___|_\__,_| .__/_\___|    | |___|\_/\_/|___|  | /_/
       \_\               /_/                 |_|              \_\               /_/
\****************************************************************************************/

struct edge{
	int v, bf;
	LL flow;
};

const int MAXN = 40010;
vector<edge> adj[MAXN];
int lvl[MAXN], st[MAXN];
const int sink = 40008, source = 40009;
int idx[1005][1005],idx1[1005][1005];
char input[1005][1005];

void addedge(int u,int v,LL flow){
	adj[u].pb({v,sz(adj[v]),flow});
	adj[v].pb({u,sz(adj[u])-1,0});
}

bool bfs(int S,int G){
	queue<int> q;
	mem(lvl,-1);
	q.push(S); lvl[S]=0;
	while(!q.empty()){
		int u = q.front(); q.pop();
		for(int i=0;i<sz(adj[u]);i++){
			int v = adj[u][i].v;
			LL flow = adj[u][i].flow;
			if(lvl[v]!=-1||flow<=0) continue;
			lvl[v]=lvl[u]+1;
			q.push(v);
		}
	}
	return (lvl[G]!=-1);
}

LL dfs(int u,LL mini,int G){
	if(u==G) return mini;
	
	for(int i=st[u];i<sz(adj[u]);i++){
		st[u]=i;
		int v = adj[u][i].v;
		LL flow = adj[u][i].flow;
		if(flow<=0||(lvl[v]-lvl[u]!=1)) continue;
		LL ret=dfs(v,min(mini,flow),G);
		if(ret>0){
			adj[u][i].flow-=ret;
			int bvf = adj[u][i].bf;
			adj[v][bvf].flow+=ret;
			return ret;
		}
	}
	return 0;
}

LL maxflow(int S,int G){
	LL ret=0;
	bool fl=false;
	while(1){
		if(!bfs(S,G)) break;
		LL flow = 0;
		mem(st,0);
		while((flow=dfs(S, LLINF, G))>0){
			ret+=flow;
			fl=true;
		}
	}
	if(!fl) return LLINF;
	return ret;
}

int main(){
	int r,c;
	scanf("%d%d",&r,&c);
	getchar();
	for(int i=0;i<r;i++){
		scanf("%s",input[i]);
	}
	
	int temp=0;
	for(int i=0;i<r;i++){
		for(int j=0;j<c;j++){
			idx[i][j]=temp;
			idx1[i][j]=temp+20000;
			temp++;
		}
	}
	
	for(int i=0;i<r;i++){
		for(int j=0;j<c;j++){
			if(input[i][j]=='.'){
				addedge(idx[i][j],idx1[i][j],1);
				
				if(i==0||i==r-1||j==0||j==c-1){
					addedge(idx1[i][j],sink,INF);
				}
			}
			else{
				addedge(source,idx[i][j],INF);
				addedge(idx[i][j],idx1[i][j],INF);
				
				if(i==0||i==r-1||j==0||j==c-1){
					printf("-1\n");
					return 0;
				}
			}
			
			if(i!=0) addedge(	idx1[i][j], idx[i-1][j],INF);
			if(i!=r-1) addedge(	idx1[i][j], idx[i+1][j],INF);
			if(j!=0) addedge(	idx1[i][j], idx[i][j-1],INF);
			if(j!=c-1)addedge(	idx1[i][j], idx[i][j+1],INF);
		}
	}
	
	LL ans = maxflow(source,sink);
	if(ans==LLINF) printf("-1\n");
	else printf("%lld\n",ans);
	
	return 0;
}

Measured memory_bytes: 6656