#include <bits/stdc++.h>
#define rei register int
using namespace std;
const int maxn = 1010;
int h,w;
string s[maxn];
queue<int>q;
int vish[maxn],visw[maxn],cnth = 2,cntw = 2;
void updateh(int x){
	for(rei i = 0;i < w;++i) if(s[x][i] == '#') {
		if(visw[i + 1] == 0){
			visw[i + 1] = 1; q.push(i + 1); ++cntw;
		}		 
	}
} void updatew(int x){
	--x; for(rei i = 1;i <= h;++i) if(s[i][x] == '#'){
		if(vish[i] == 0){
			vish[i] = 1; q.push(i); ++cnth;
		}
	}
} void bfsh(int x){
	q.push(x); vish[x] = 1;
	while(!q.empty()){
		int x = q.front(); q.pop(); if(vish[x]) updateh(x);
		if(visw[x]) updatew(x);
	}
} void bfsw(int x){
	q.push(x); visw[x] = 1;
	while(!q.empty()){
		int x = q.front(); q.pop(); if(vish[x]) updateh(x);
		if(visw[x]) updatew(x);
	}
} void work(){
	vish[1] = vish[h] = visw[1] = visw[w] = 1;
	updateh(1),updateh(h),updatew(1),updatew(w);
	while(!q.empty()){
		int x = q.front(); q.pop(); if(vish[x]) updateh(x);
		if(visw[x]) updatew(x);
	}
}
int main(){
	ios::sync_with_stdio(false); cin >> h >> w; 
	for(rei i = 1;i <= h;++i){
		cin >> s[i] ; 
	} int ans = 99999,cnt = 0;
	work(); for(rei i = 1;i <= h;++i) if(!vish[i]) bfsh(i),++cnt; ans = cnt; cnt = 0;
	memset(vish,0,sizeof(vish)),memset(visw,0,sizeof(visw));  work(); for(rei i = 1;i <= w;++i)
	if(!visw[i]) bfsw(i),++cnt ; ans = min(ans,cnt); cout << ans ; 
	return 0;
}
#include<bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,mmx,avx,avx2")
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define req(i,a,b) for(int i=(a);i>=(b);--i)
#define rep_(i,a,b) for(int i=(a);i<(b).size();++i)
#define F(a) rep(a,1,n)
#define M(a,b) memset(a,b,sizeof a)
#define DC int T;cin>>T;while(T--)
#define ll long long
#define Z(a,b) sort(a+1,a+b+1)
using namespace std;
const unsigned _mod=998244353;
const unsigned mod=1e9+7;
const ll infi=0x3f3f3f3f3f3f3fll;
const int inf=0x3f3f3f3f;
void rd(auto &x){x=0;int f=1;char ch=getchar();while(ch<48||ch>57){if(ch==45)f=-1;ch=getchar();}while(ch>=48&&ch<=57)x=x*10+ch-48,ch=getchar();x*=f;}
ll ksm(ll x,ll y=mod-2,ll m=mod){ll ret=1;while(y){if(y&1)ret=ret*x%m;y>>=1ll;x=x*x%m;}return ret;}
ll qpow(ll x,ll y){ll ret=1;while(y){if(y&1ll)ret=ret*x;y>>=1ll;x=x*x;}return ret;}
/*
    [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
    [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
    [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
*/
int h,w,fa[2010],s,t;
char c;
unordered_map<int,bool> mp;
inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
inline void merge(int x,int y){fa[find(x)]=find(y);}
int main()
{
    cin>>h>>w;
    rep(i,1,h+w) fa[i]=i;
    merge(1,h+1),merge(1,h+w),merge(h,h+1),merge(h,h+w);
    rep(i,1,h) rep(j,1,w) cin>>c,c=='#'&&(merge(i,h+j),1);
    rep(i,1,h) s+=!mp[find(i)],mp[find(i)]=1;
	mp.clear();
	rep(i,h+1,h+w) t+=!mp[find(i)],mp[find(i)]=1;
	cout<<min(s,t)-1<<'\n';
    return 0;
}