#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

typedef long long ll;

ll n, m;
bool v[1005][1005];
ll cp[2005];
bool v1[2005], v2[2005];
ll cnt1, cnt2;

ll fnd(ll p)
{
    if(cp[p] == p) return p;
    return cp[p] = fnd(cp[p]);
}

void uni(ll p, ll q)
{
    if(fnd(p) == fnd(q)) return;
    cp[fnd(p)] = fnd(q);
}

int main()
{
    scanf("%lld%lld", &n, &m);
    for(ll i = 0; i < n + m; i++) cp[i] = i;
    for(ll i = 0; i < n; i++)
    {
        string s;
        cin>>s;
        for(ll j = 0; j < m; j++)
        {
            if(s[j] == '#') v[i][j] = true, uni(i, j + n);
        }
    }
    uni(0, n - 1);
    uni(0, n);
    uni(0, m + n - 1);
    for(ll i = 0; i < n; i++)
    {
        if(!v1[fnd(i)]) cnt1++, v1[fnd(i)] = true;
    }
    for(ll i = 0; i < m; i++)
    {
        if(!v2[fnd(i + n)]) cnt2++, v2[fnd(i + n)] = true;
    }
    printf("%lld\n", min(cnt1 - 1, cnt2 - 1));
    return 0;
}

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<n;++i)
#define repn(i,n) for(int i=1;i<=n;++i)
#define LL long long
#define pii pair <int,int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD=1e9+7;

int n,m,ans=0;
string s[1010];
bool visr[1010],visc[1010],vis[1010][1010],exbr[1010],exbc[1010];
vector <int> gr_c[1010],gc_r[1010];
vector <pair <vector <int>,vector <int> > > v;
pair <vector <int>,vector <int> > tmp;
queue <pii> q;

void bfs(int x,int y)
{
  q.push(mpr(x,y));vis[x][y]=visr[x]=visc[y]=true;
  while(!q.empty())
  {
    pii f=q.front();q.pop();
    if(!exbr[f.fi])
    rep(i,gr_c[f.fi].size()) 
    {
      if(vis[f.fi][gr_c[f.fi][i]]) continue;
      q.push(mpr(f.fi,gr_c[f.fi][i]));
      vis[f.fi][gr_c[f.fi][i]]=visc[gr_c[f.fi][i]]=true;
    }
    exbr[f.fi]=true;
    if(!exbc[f.se])
    rep(i,gc_r[f.se].size()) 
    {
      if(vis[gc_r[f.se][i]][f.se]) continue;
      q.push(mpr(gc_r[f.se][i],f.se));
      vis[gc_r[f.se][i]][f.se]=visr[gc_r[f.se][i]]=true;
      //cout<<gc_r[f.se][i]<<'h'<<endl;
    }
    exbc[f.se]=true;
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin>>n>>m;
  rep(i,n) cin>>s[i];
  rep(i,n) rep(j,m)
  {
    if(s[i][j]=='.') continue;
    gr_c[i].pb(j);gc_r[j].pb(i);
  }
  rep(i,n) gr_c[i].pb(0),gr_c[i].pb(m-1);
  rep(i,m) gc_r[i].pb(0),gc_r[i].pb(n-1);

  bfs(0,0);
  rep(i,n) rep(j,m) if(s[i][j]=='#'&& !vis[i][j]) bfs(i,j),++ans;
  LL res1=0,res2=0;
  rep(i,n) if(!visr[i]) ++res1;rep(i,m) if(!visc[i]) ++res2;
  ans+=min(res1,res2);
  cout<<ans<<endl;

	return 0;
}