#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define fi first
#define se second
#define mset(a,b) memset(a, b, sizeof(a))
#define dbg(x) cout << "[" << #x << "]: " << x << endl;
#define forn(i,n) for(int i=0; i < n;i++)
#define forab(i,a,b) for(int i = a; i <= b; i++)
#define forba(i,b,a) for(int i = b; i >= a; i--)
#define each(val, v) for(auto val : v)
#define abs(u) (u >= 0 ? u : -u)

using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vl = vector<ll>;

const int MAXN = 2e6 + 5;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;

int uf[MAXN];
int sz[MAXN];

int find(int u){
    if( uf[u] == u ) return u;

    return uf[u] = find(uf[u]);
}

int uni(int a, int b){
    int aa= find(a), bb = find(b);

    if(aa == bb) return 0;

    if(sz[aa] > sz[bb]) swap(aa,bb);

    uf[aa] = bb;
    sz[bb] += sz[aa];

    return 1;
}

int col(int i){
    return i + 1005; 
}

int line(int i){
    return i;
}


void solve(){
    int n,m;
    cin >> n >> m;

    forn(i, 5000){
        uf[i] = i;
    }
    uni( col(0), line(0) );
    uni( col(m-1), line(n-1) );

    uni(col(0), col(m-1));

    forn(i,n){
        string s;
        cin>> s;
        forn(j,m){
            if(s[j] == '#'){
                uni( line(i), col(j) );
            }
        }
    }

    set<int> lines;
    set<int> cols;
    int ans = n + m;

    forn(i,n){
        int x = find(line(i));
        

        lines.insert( x);
    }

    forn(i,m){
        
        int x = find( col(i) );
        cols.insert( x );
    }

    int sz = lines.size();
    ans = min(ans, sz -1);
    sz = cols.size();
    ans = min(ans, sz-1);

    cout << ans << '\n';


}

int main(){

    ios::sync_with_stdio(false); cin.tie(NULL);

    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }

    return 0;
}
//
// Created by mihai145 on 23.02.2021.
//

#include <iostream>
#include <vector>
#include <string>

using namespace std;

const int NMAX = 1000;

int N, M;
vector<int> g[2 * NMAX + 5];
bool vis[2 * NMAX + 5];

void dfs(int node) {
    vis[node] = true;

    for(auto it : g[node]) {
        if(!vis[it]) {
            dfs(it);
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> N >> M;

    for(int i = 0; i < N; i++) {
        string s;
        cin >> s;

        for(int j = 0; j < M; j++) {
            if(s[j] == '#') {
                g[i + 1].push_back(N + j + 1);
                g[N + j + 1].push_back(i + 1);
            }
        }
    }

    g[1].push_back(N + 1);
    g[N + 1].push_back(1);

    g[1].push_back(N + M);
    g[N + M].push_back(1);

    g[N].push_back(N + 1);
    g[N + 1].push_back(N);

    g[N].push_back(N + M);
    g[N + M].push_back(N);

    int compN = 0, compM = 0;

    for(int i = 1; i <= N; i++)
        if(!vis[i]) {
            compN++;
            dfs(i);
        }

    for(int i = 1; i <= N + M; i++)
        vis[i] = false;

    for(int i = 1; i <= M; i++)
        if(!vis[i + N]) {
            compM++;
            dfs(i + N);
        }

    cout << min(compN, compM) - 1 << '\n';

    return 0;
}