#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
#define read(a) scanf("%d", &a)
#define x first
#define y second

#define N 1011

int n, m;
int ans;
char a[N][N], b[N][N];

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
bool vis[N];
bool v[N][N][4];

struct state {
	int x, y, d;
};

queue <state> q;
bool ok(int x, int y) {
	return 1 <= x && x <= n && 1 <= y && y <= m;
}

void check(char g[N][N]) {
	while (!q.empty()) {
		auto o = q.front(); q.pop();
		int x = o.x, y = o.y, d = o.d;
		if (g[x][y] == '#' || !ok(x + dx[d], y + dy[d])) {
			vis[y] = 1;
			for (int i = 0; i < 4; i++) {
				v[x][y][i] = 1;
			}
			for (d = 0; d < 4; d++) {
				if (ok(x + dx[d], y + dy[d])) {
					if ( !v[x + dx[d]][y + dy[d]][d] ) {
						v[x + dx[d]][y + dy[d]][d] = 1;
						q.push({x + dx[d], y + dy[d], d});
					}
				}
			}
		}
		else if (ok(x + dx[d], y + dy[d])) {
			if ( !v[x + dx[d]][y + dy[d]][d] ) {
				v[x + dx[d]][y + dy[d]][d] = 1;
				q.push({x + dx[d], y + dy[d], d});
			}
		}
	}
}
void work(char g[N][N]) {
	memset(vis, 0, sizeof vis);
	vis[1] = 1; vis[m] = 1;
	memset(v, 0, sizeof v);
	int tot = 0;
	v[1][1][0] = 1;
	q.push({1, 1, 0});
	v[1][1][1] = 1;
	q.push({1, 1, 1});
	check(g);

	vector <int> op;
	for (int i = 1; i <= m; i++) if (!vis[i]) {
		op.push_back(i);
		g[1][i] = '#';
		tot++;
		q.push({1, i, 0});
		check(g);
	}

	for (auto x : op) {
		g[1][x] = '.';
	}
	ans = min(ans, tot);
}

int main() {
	read(n); read(m);
	ans = n;
	for (int i = 1 ;i <= n; i++) {
		scanf("%s", a[i] + 1);
	}
	work(a);

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			b[j][i] = a[i][j];
		}
	}
	swap(n, m);
	work(b);

	printf("%d\n", ans);

}

//W4P3R
#include<bits/stdc++.h>
#define inf 1e9
#define eps 1e-6
#define mp make_pair
#define pb push_back
#define re register ll
#define fr first
#define sd second
#define pa pair<ll,ll>
#define FOR(i,a,b) for(re i=a;i<=b;i++)
#define REP(i,a,b) for(re i=a;i>=b;i--)
#define MEM(a) memset(a,0,sizeof(a))
#define N 2010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
inline ll read()
{
	char ch=getchar();
	ll s=0,w=1;
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}
	return s*w;
}
inline ll lowbit(ll x){return x&(-x);}
char s[N][N];
int n,m,vis[N],fa[N];
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
inline void add(int x,int y){x=find(x),y=find(y);fa[x]=y;}
int main()
{
	//ios::sync_with_stdio(false);
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	n=read(),m=read();
	FOR(i,1,n+m)fa[i]=i;
	FOR(i,1,n)
	{
		scanf("%s",s[i]+1);
		FOR(j,1,m)if(s[i][j]=='#')add(i,j+n);
	}
	add(1,n+1);add(1,n+m);add(1,n);
	int ans=0,num=0;
	FOR(i,1,n)if(!vis[find(i)])vis[find(i)]=1,num++;
	ans=num-1;MEM(vis);num=0;
	FOR(i,1,m)if(!vis[find(i+n)])vis[find(i+n)]=1,num++;
	ans=min(ans,num-1);MEM(vis);
	cout<<ans<<'\n';
	return 0;
}
//gl

