#include<bits/stdc++.h> 
using namespace std;
const int N=1e6+10,M=1e3+5;
int n,m,par[N],sum[M][M],add[M][M],res;
char s[M][M];
bool vis[N];
set<int>x,y;
int find(int x){
	return par[x]==x?x:par[x]=find(par[x]); 
}
int f(int x,int y){
	return x*1000+y;
}
void un(int x,int y){
	x=find(x),y=find(y);
	if(x==y)return;
	if(x>y)swap(x,y);
	par[y]=x;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;++i){
		scanf("%s",s[i]);
		for(int j=0;j<m;++j){
			int v=f(i,j);
			par[v]=v;
		}
	}
	s[0][0]=s[n-1][0]=s[0][m-1]=s[n-1][m-1]='#';
	for(int i=0;i<n;++i){
		for(int j=0;j<m;++j){
			if(s[i][j]=='#'){
				add[i][0]++;add[i][m]--;
				add[i+1][0]--;add[i+1][m]++;
				add[0][j]++;add[n][j]--;
				add[0][j+1]--;add[n][j+1]++;
				for(int k=i+1;k<n;++k)if(s[k][j]=='#'){un(f(i,j),f(k,j));break;}
				for(int k=j+1;k<m;++k)if(s[i][k]=='#'){un(f(i,j),f(i,k));break;}
			}
		}
	}
	for(int i=0;i<n;++i){
		for(int j=0;j<m;++j){
			if(i-1>=0)sum[i][j]+=sum[i-1][j];
			if(j-1>=0)sum[i][j]+=sum[i][j-1];
			if(i-1>=0 && j-1>=0)sum[i][j]-=sum[i-1][j-1];
			sum[i][j]+=add[i][j];
			//printf("i:%d j:%d v:%d\n",i,j,sum[i][j]);
			if(!sum[i][j]){
				x.insert(i);
				y.insert(j);
			}
			int v=f(i,j);
			if(s[i][j]=='#' && par[v]==v)res++;
		}
	}
	printf("%d\n",res-1+min(x.size(),y.size()));
	return 0;
}
// This code wrote by chtholly_micromaker(MicroMaker)
#include <bits/stdc++.h>
#define reg register
#define int long long
#define ALL(x) (x).begin(),(x).end()
#define mem(x,y) memset(x,y,sizeof x)
#define sz(x) (int)(x).size()
#define ln std::puts("")
#define lsp std::putchar(32)
#define pb push_back
#define MP std::make_pair
#define dbg(x) std::cerr<<__func__<<"\tLine:"<<__LINE__<<' '<<#x<<": "<<x<<"\n"
#define dprintf(x...) std::fprintf(stderr,x)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,b,a) for(int i=(b);i>=(a);--i)
const int MaxN=1050;
template <class t> inline void read(t &s){s=0;
reg int f=1;reg char c=getchar();while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c))s=(s<<3)+(s<<1)+(c^48),c=getchar();s*=f;return;}
template<class t,class ...A> inline void read(t &x,A &...a){read(x);read(a...);}
template <class t> inline void write(t x){if(x<0)putchar('-'),x=-x;
int buf[21],top=0;while(x)buf[++top]=x%10,x/=10;if(!top)buf[++top]=0;
while(top)putchar(buf[top--]^'0');return;}
inline void setIn(std::string s){freopen(s.c_str(),"r",stdin);return;}
inline void setOut(std::string s){freopen(s.c_str(),"w",stdout);return;}
inline void setIO(std::string s=""){setIn(s+".in");setOut(s+".out");return;}
template <class t>inline bool checkmin(t&x,t y){if(x>y){x=y;return 1;}return 0;}
template <class t>inline bool checkmax(t&x,t y){if(x<y){x=y;return 1;}return 0;}
inline int lowbit(int x){return x&(-x);}
char s[MaxN][MaxN];
int f[MaxN<<1];
inline int getf(int x)
{
	return f[x]==x?x:f[x]=getf(f[x]);
}
inline void merge(int x,int y)
{
	reg int a=getf(x),b=getf(y);
	if(a!=b)
		f[b]=a;
}
signed main(void)
{
	int n,m;read(n,m);
	for(int i=1;i<=n;++i)
		std::scanf("%s",s[i]+1);
	s[1][1]=s[n][1]=s[1][m]=s[n][m]='#';
	for(int i=1;i<=n+m;++i)
		f[i]=i;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
			if(s[i][j]=='#')
				merge(i,j+n);
	std::unordered_map<int,int> r,c;
	for(int i=1;i<=n;++i)
		r[getf(i)];
	for(int i=n+1;i<=n+m;++i)
		c[getf(i)];
	write(std::min((int)r.size(),(int)c.size())-1),ln;
	return 0;
}

/*
 * Check List:
 * 1. Input / Output File (OI)
 * 2. long long 
 * 3. Special Test such as n=1
 * 4. Array Size
 * 5. Memory Limit (OI) int is 4 and longlong is 8
 * 6. Mod (a*b%p*c%p not a*b*c%p  ,  (a-b+p)%p not a-b )
 * 7. Name ( int k; for(int k...))
 * 8. more tests , (T=2 .. more)
 * 9. blank \n after a case
*/



