#include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
#include <queue>

using namespace std;

#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define FF first
#define SS second
#define pb push_back
#define mp make_pair

typedef long long int LL;
typedef pair<int, int > pii;
typedef vector<int > vi;

const int N = 1001;

string s;
vi g[2 * N];
int vis[2 * N];
set<int > s1, s2;

void dfs(int c, int cmp) {
  if(vis[c]) return;
  vis[c] = cmp;
  rep(i,0,g[c].size()) {
    dfs(g[c][i], cmp);
  }
}

int main() {
  int n,m;
  S2(n,m);
  rep(i,0,n) {
    cin >> s;
    rep(j,0,m) if(((i == 0 || i == n - 1) && (j == 0 || j == m - 1)) ||s[j] == '#') {
      g[i + 1].pb(n + j + 1);
      g[n + j + 1].pb(i + 1);
    }
  }
  int cnt = 0;
  rep(i,1,n+m+1) if(!vis[i]) {
    ++cnt;
    dfs(i, cnt);
  }
  rep(i,1,n+m+1) {
    if(i <= n) s1.insert(vis[i]);
    else s2.insert(vis[i]);
  }
  P((int)min(s1.size(), s2.size()) - 1);
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define zep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(int i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

struct union_find {
    vector<int> par, siz;
    union_find(int n) {
        par.resize(n);
        siz.resize(n);
        for(int i = 0; i < n; i++){
            par[i] = i;
            siz[i] = 1;
        }
    }
    
    int find(int x) {
        if (par[x] == x) return x;
        return par[x] = find(par[x]);
    }
 
    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y)return;
        if (siz[x] < siz[y]) {
            par[x] = y;
            siz[y] += siz[x];
        }
        else {
            par[y] = x;
            siz[x] += siz[y];
        }
    }
    
    long long size(int x){
        return siz[find(x)];
    }

    bool same(int x, int y) {
        return (find(x) == find(y));
    }
};

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    
    ll h, w; cin >> h >> w;
    string s[h];
    zep(i, 0, h)cin >> s[i];

    union_find u(h + w);

    u.unite(0, h - 1);
    u.unite(0, h);
    u.unite(0, h + w - 1);

    zep(i, 0, h){
        zep(j, 0, w){
            if(s[i][j] == '#'){
                u.unite(i, h + j);
            }
        }
    }

    set<ll> ansh, answ;
    zep(i, 0, h)ansh.insert(u.find(i));
    zep(j, 0, w)answ.insert(u.find(h + j));
    print(min(ansh.size(), answ.size()) - 1)
    
    return 0;
}