#include<bits/stdc++.h>
using namespace std;
//dengyaotriangle!

const int maxn=1005*2;

int n,m;
vector<int> adj[maxn];
bool vis[maxn];

void adde(int a,int b){
    adj[a].push_back(b);
    adj[b].push_back(a);
}
int dfs(int u){
    vis[u]=1;
    int msk=1<<(u<=n);
    for(int i=0;i<(int)adj[u].size();i++){
        int v=adj[u][i];
        if(!vis[v])msk|=dfs(v);
    }
    return msk;
}
int cnt[2];
int main(){
    ios::sync_with_stdio(0);cin.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        char _[maxn];
        cin>>_;
        for(int j=1;j<=m;j++){
            if(_[j-1]=='#')adde(i,j+n);
        }
    }
    adde(1,1+n);adde(n,1+n);adde(1,n+m);adde(n,n+m);
    for(int i=1;i<=n+m;i++)if(!vis[i]){
        int cw=dfs(i);
        for(int j=0;j<2;j++)cnt[j]+=(cw>>j)&1;
    }
    cout<<min(cnt[0],cnt[1])-1;
    return 0;
}
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <cstdlib>

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define li long long
#define pii pair<int, int>
#define vi vector<int>
#define li long long

#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define fore(i, b, e) for (int i = (int)b; i <= (int)e; i++)

const int MAXN = 2005;

bool visited[MAXN];
vi edges[MAXN];
int n, m;

void dfs(int v) {
	visited[v] = true;
	for (int u : edges[v]) {
		if (!visited[u])
			dfs(u);
	}
}

void add_edge(int row, int col) {
	edges[row].pb(col + n);
	edges[col + n].pb(row);
}

int main() {
	scanf("%d%d", &n, &m);
	forn(i, n) {
		string s;
		cin >> s;
		forn(j, m) {
			if (s[j] == '#') {
				add_edge(i, j);
			}
		}
	}
	/*forn(i, n) {
		add_edge(i, 0);
		add_edge(i, m - 1);
	}
	forn(j, m) {
		add_edge(0, j);
		add_edge(n - 1, j);
	}*/
	add_edge(0, 0);
	add_edge(0, m);
	add_edge(n - 1, 0);
	add_edge(n - 1, m - 1);
	int row_comp = 0;
	forn(i, n) {
		if (!visited[i]) {
			++row_comp;
			dfs(i);
		}
	}
	memset(visited, false, sizeof(visited));
	int col_comp = 0;
	fore(i, n, n + m - 1) {
		if (!visited[i]) {
			++col_comp;
			dfs(i);
		}
	}
	//cerr << row_comp << " " << col_comp << endl;
	printf("%d", min(row_comp - 1, col_comp - 1));
}