#include <bits/stdc++.h>
using namespace std;
int h,w;
string s[1003];
int f[2003];
int F(int x){
	return f[x]==x?x:f[x]=F(f[x]);
}
int main(){
	cin>>h>>w;
	for(int i=0;i<h;i++)cin>>s[i];
	s[0][0]=s[0][w-1]=s[h-1][0]=s[h-1][w-1]='#';
	for(int i=0;i<h+w;i++)f[i]=i;
	for(int i=0;i<h;i++)for(int j=0;j<w;j++)
		if(s[i][j]=='#'){
			int x=F(i),y=F(j+h);		
			if(x!=y)f[x]=y;
		}
	unordered_set<int>R,C;
	for(int i=0;i<h;i++)R.insert(F(i));
	for(int i=0;i<w;i++)C.insert(F(i+h));
	cout<<min(R.size(),C.size())-1;
}
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define x first
#define y second
typedef pair<int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x,T y){x<y?x=y:T();}
template <typename T> void chkmin(T &x,T y){x>y?x=y:T();}
template <typename T> void readint(T &x)
{
	x=0;int f=1;char c;
	for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1;
	for(;isdigit(c);c=getchar())x=x*10+c-'0';
	x*=f;
}
const int MOD=998244353;
inline int dmy(int x){return x>=MOD?x-MOD:x;}
inline void inc(int &x,int y){x=dmy(x+y);}
inline int qmi(int x,int y)
{
	int ans=1;
	for(;y;y>>=1,x=1ll*x*x%MOD)
		if(y&1)ans=1ll*ans*x%MOD;
	return ans;
}
const int MAXN=1005;

int n,m;
char a[MAXN][MAXN];
int fa[MAXN<<1],sx[MAXN<<1],sy[MAXN<<1];
int getfa(int u){return u==fa[u]?u:fa[u]=getfa(fa[u]);}
void merge(int u,int v)
{
	u=getfa(u),v=getfa(v);
	if(u!=v)fa[u]=v,sx[v]+=sx[u],sy[v]+=sy[u];
}

int main()
{
	#ifdef LOCAL
	freopen("code.in","r",stdin);
//	freopen("code.out","w",stdout);
	#endif
	readint(n),readint(m);
	for(int i=1;i<=n+m;++i)fa[i]=i,sx[i]=(i<=n),sy[i]=(i>n);
	for(int i=1;i<=n;++i)
	{
		scanf("%s",a[i]+1);
		for(int j=1;j<=m;++j)
			if(a[i][j]=='#')merge(i,j+n);
	}
	merge(1,n+1);merge(1,n+m);merge(n,n+1);merge(n,n+m);
	int rx=-1;
	for(int i=1;i<=n+m;++i)if(getfa(i)==i && sx[i])++rx;
	int ry=-1;
	for(int i=1;i<=n+m;++i)if(getfa(i)==i && sy[i])++ry;
	printf("%d\n",min(rx,ry));
	return 0;
}
