#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define sz(v) (v).size()
#define all(v) (v).begin(),(v).end()
#define REP(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Time (double)clock()/CLOCKS_PER_SEC
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pi;
template <class T> void chmax(T &x,T y) {x=x>y?x:y;return;}
template <class T> void chmin(T &x,T y) {x=x<y?x:y;return;}
template <class T> T sqr(T x) {return x*x;}
template <class T> T gcd(T x,T y) {return y==0?x:gcd(y,x%y);}
template <class T> T lcm(T x,T y) {return x/gcd(x,y)*y;}
const int inf=1000000000,mod=1000000007;
const ll infll=1000000000000000000ll,modll=ll(mod);
const db maxdb=db(infll),eps=1e-14,PI=acos(-1.0);
//#define mydef
//#define LOCAL
#ifdef mydef
#define foreach(v,it) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#endif
#define debug
#define putnum(x) cout<<#x<<": "<<x<<endl
#define putvec(v) cout<<#v<<": ";REP(i,0,sz(v)) cout<<v[i]<<" ";cout<<endl
template <class T> inline void read(T &x){
	T w=1;x=0;char c=getchar();
	while(c<'0'||c>'9') {if(c=='-')w=-1;c=getchar();}
	while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c&15);c=getchar();}
	x*=w;
}
template <class T> inline void write(T x) {
	if(x<0) putchar('-'),x=~x+1;
	if(x) write(x/10),putchar(x%10+'0');
}

const int maxn=1005;
const int dx[4]={-1,0,0,1},dy[4]={0,-1,1,0};
int h,w,ans1,ans2;
int cnt1[maxn],cnt2[maxn];
int a[maxn][maxn],vis[maxn][maxn][4],hav[maxn][maxn];
string str;

struct point {
	int x,y;
	int dir;
};
queue<point> q;

bool cango(point pos) {
	if(pos.x<1||pos.y<1||pos.x>h||pos.y>w) return false;
	return true;
}

vector<int> v1[maxn],v2[maxn];

void dfs1(int pos) {
	cnt1[pos]=0;
	for(int i=0;i<sz(v1[pos]);i++) {
		int to=v1[pos][i];
		if(!cnt1[to]) continue;
		dfs1(to);
	}
	return;
}

void dfs2(int pos) {
	cnt2[pos]=0;
	for(int i=0;i<sz(v2[pos]);i++) {
		int to=v2[pos][i];
		if(!cnt2[to]) continue;
		dfs2(to);
	}
	return;
}

int main() {
	#ifdef LOCAL
		freopen("input.in","r",stdin);
		freopen("output.out","w",stdout);
	#endif
	scanf("%d%d",&h,&w);
	for(int i=1;i<=h;i++) {
		cin>>str;
		for(int j=1;j<=w;j++) {
			if(str[j-1]=='#')
				a[i][j]=1;
		}
	}
	for(int i=0;i<4;i++) {
		q.push((point){1,1,i});
		vis[1][1][i]=1;
	}
	while(!q.empty()) {
		point pos=q.front();
		hav[pos.x][pos.y]=1;
		q.pop();
		point to=pos;
		to.x+=dx[to.dir];
		to.y+=dy[to.dir];
		if(!cango(to)) continue;
		point ag=to;
		ag.x+=dx[to.dir];
		ag.y+=dy[to.dir];
		if(!cango(ag)||a[to.x][to.y]==1) {
			for(int i=0;i<4;i++) {
				if(!vis[to.x][to.y][i]) {
					q.push((point){to.x,to.y,i});
					vis[to.x][to.y][i]=1;
				}
			}
		} else {
			if(!vis[to.x][to.y][to.dir]) {
				q.push(to);
				vis[to.x][to.y][to.dir]=1;
			}
		}
	}
/*	for(int i=1;i<=h;i++) {
		for(int j=1;j<=w;j++) {
			cout<<hav[i][j]<<" "; 
		}
		cout<<endl;
	}*/
	
	for(int i=1;i<=h;i++)
		for(int j=1;j<=w;j++)
			if(!hav[i][j])
				cnt1[i]=1,cnt2[j]=1;
	for(int i=1;i<=h;i++) {
		int is=0;
		for(int j=1;j<=w;j++) {			
			if(hav[i][j]) continue;
			if(is&&a[i][j]) {
				v2[j].pb(is);
				v2[is].pb(j);			
			}
			if(a[i][j]) is=j;
		}
	}
	for(int i=1;i<=w;i++) {
		int is=0;
		for(int j=1;j<=h;j++) {
			if(hav[j][i]) continue;
			if(is&&a[j][i]) {
				v1[j].pb(is);
				v1[is].pb(j);
			}
			if(a[j][i]) is=j;
		}
	}
	for(int i=1;i<=h;i++) {
		if(cnt1[i]) {
			ans1++;
			dfs1(i);
		}
	}
	for(int i=1;i<=w;i++) {
		if(cnt2[i]) {
			ans2++;
			dfs2(i);
		}
	}
	cout<<min(ans1,ans2)<<endl;
	return 0;
}

#include <bits/stdc++.h>
#define pb push_back
#define fst first
#define snd second
#define fore(i,a,b) for(int i=a,ggdem=b;i<ggdem;++i)
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(),x.end()
#define mset(a,v) memset((a),(v),sizeof(a))
#define FIN ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;

const ll N=1010;

char c[1010][1010];
char d[1010][1010];

/*---------------- Union Find (DSU) ------------------*/
int pa[N],sz[N];
ll find_pa(ll pos){
	if(pa[pos]!=pos)pa[pos]=find_pa(pa[pos]);
	return pa[pos];
}
void join(int a, int b) {
	int x=find_pa(a),y=find_pa(b);
	if (x!=y) {
		if(sz[x]>=sz[y]) sz[x]+=sz[y],pa[y]=x;
		else sz[y]+=sz[x],pa[x]=y;
	}
}
void dsu_init(ll n){fore(i,0,n)pa[i]=i,sz[i]=1;}

int main(){FIN;
	ll n,m; cin>>n>>m;
	fore(i,0,n){
		fore(j,0,m){
			cin>>c[i][j];
		}
	}
	c[0][0]='#';
	c[0][m-1]='#';
	c[n-1][0]='#';
	c[n-1][m-1]='#';
	dsu_init(n);
	fore(j,0,m){
		vector<ll> v;
		fore(i,0,n){
			if(c[i][j]=='#')v.pb(i);
		}
		fore(i,0,SZ(v)-1)join(v[i],v[i+1]);
	}
	ll res0=0;
	fore(i,1,n){
		if(find_pa(0)!=find_pa(i)){
			res0++;
			join(0,i);
		}
	}
	fore(i,0,n){
		fore(j,0,m){
			d[j][i]=c[i][j];
		}
	}
	swap(n,m);
	fore(i,0,n){
		fore(j,0,m){
			c[i][j]=d[i][j];
		}
	}
	dsu_init(n);
	fore(j,0,m){
		vector<ll> v;
		fore(i,0,n){
			if(c[i][j]=='#')v.pb(i);
		}
		fore(i,0,SZ(v)-1)join(v[i],v[i+1]);
	}
	ll res1=0;
	fore(i,1,n){
		if(find_pa(0)!=find_pa(i)){
			res1++;
			join(0,i);
		}
	}
	cout<<min(res0,res1)<<"\n";
	
	return 0;
}