#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ~ ";
	dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {";  \
	for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif
struct UnionFind{
	vector<int> par,rank,hasL,hasR,marked;
	UnionFind(int N){
		par.assign(N,0);
		rank.assign(N,0);
		hasL.resize(N);hasR.resize(N);marked.resize(N);
		rep(i,N) par[i]=i;
	}
	int find(int x){
		if(par[x]==x) return x;
		return par[x]=find(par[x]);
	}
	bool same(int x,int y){
		return find(x)==find(y);
	}
	void unite(int x,int y){
		x=find(x),y=find(y);
		if(x==y) return;
		if(rank[x]<rank[y]) swap(x,y);

		//x becomes root
		par[y]=x;
		if(rank[x]==rank[y]) rank[x]++;
		hasL[x] += hasL[y];
		hasR[x] += hasR[y];
		marked[x] += marked[y];
	}
};
int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);

	int H,W; cin >> H >> W;
	V<string> s(H); rep(i,H) cin >> s[i];

	UnionFind UF(H+W);
	rep(i,H) UF.hasL[i] = 1;
	rep(i,W) UF.hasR[i+H] = 1;
	UF.marked[0] = true;
	rep(i,H) rep(j,W) if(s[i][j] == '#'){
		UF.unite(i,j+H);
	}
	for(int i:{0,H-1}) for(int j:{0,W-1}) UF.unite(i,j+H);
	int ans = 1e9;
	{
		int waf = 0;
		rep(v,H+W) if(UF.find(v) == v && UF.hasL[v] && !UF.marked[v]) waf++;
		chmin(ans,waf);
	}
	{
		int waf = 0;
		rep(v,H+W) if(UF.find(v) == v && UF.hasR[v] && !UF.marked[v]) waf++;
		chmin(ans,waf);
	}
	cout << ans << endl;
}

//starusc
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
inline int read(){
	int x=0,f=1,c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
	return f==1?x:-x;
}
const int M=1004,N=1e6+4;
struct edge{
	int v,nxt;
}e[N<<2];
int first[N],cnt;
inline void addedge(int u,int v){
	e[++cnt].v=v;e[cnt].nxt=first[u];first[u]=cnt;
}
struct node{
	int x,y,id;
}a[N];
inline bool comp_x(const node &a,const node &b){
	return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
inline bool comp_y(const node &a,const node &b){
	return a.y<b.y||(a.y==b.y&&a.x<b.x);
}
inline bool comp_id(const node &a,const node &b){
	return a.id<b.id;
}
int n,m,tot,cx,cy,rx,ry,vx[M],vy[M],vis[N];
char s[M];
void dfs(int x){
	vis[x]=1;
	if(!vx[a[x].x])cx++;
	if(!vy[a[x].y])cy++;
	vx[a[x].x]=vy[a[x].y]=1;
	for(int i=first[x];i;i=e[i].nxt)
		if(!vis[e[i].v])
			dfs(e[i].v);
}
int main(){
	n=read();m=read();
	for(int i=1;i<=n;i++){
		scanf("%s",s+1);
		for(int j=1;j<=m;j++){
			if(s[j]=='#'){
				a[++tot].x=i;a[tot].y=j;a[tot].id=tot;
			}
		}
	}
	sort(a+1,a+tot+1,comp_x);
	for(int i=1;i<=tot;i++){
		if(a[i].x==a[i-1].x){
			addedge(a[i].id,a[i-1].id);
			addedge(a[i-1].id,a[i].id);
		}
	}
	sort(a+1,a+tot+1,comp_y);
	for(int i=1;i<=tot;i++){
		if(a[i].y==a[i-1].y){
			addedge(a[i].id,a[i-1].id);
			addedge(a[i-1].id,a[i].id);
		}
	}
	rx=n-2;ry=m-2;
	vx[1]=vx[n]=vy[1]=vy[m]=1;
	sort(a+1,a+tot+1,comp_id);
	for(int i=1;i<=tot;i++)if(!vis[i]&&(a[i].x==1||a[i].y==1||a[i].x==n||a[i].y==m)){
		cx=cy=0;
		dfs(i);
		rx-=cx;ry-=cy;
	}
	for(int i=1;i<=tot;i++)if(!vis[i]){
		cx=cy=0;
		dfs(i);
		rx-=cx-1;ry-=cy-1;
	} 
	cout<<min(rx,ry)<<"\n";
	return (0-0);
}