#include<bits/stdc++.h>
using namespace std;
 
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         cout<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define pw(b,p)         pow(b,p) + 0.1
#define endl            "\n"
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());


void __print(int32_t x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
 
char myHeap[200 * 1024 * 1024];
int sz = 0;
void assert_tle(bool q) { while (!q); }
void* operator new  ( std::size_t count ) {
    sz += count;
    assert_tle(sz <= 200 * 1024 * 1024);
    return myHeap + sz - count;
}
 
void operator delete (void *ptr) { }
 
 
void fastIO()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}

struct djsets
{
    vector<int> leader;
    vector<int> rank;

    void init(int n)
    {
        rank.assign(n,1);
        for(int i=0;i<n;i++)
        {
            leader.push_back(i);
        }
    }

    int retleader(int node)
    {
        if(leader[node]==node)
            return node;
        int p= retleader(leader[node]);
        leader[node]=p;

        return p;
    }
    void connect(int a,int b)
    {
        a=retleader(a);
        b=retleader(b);

        if(rank[a]>rank[b])
        {
            leader[b]=a;
        }
        else if(rank[b]>rank[a])
        {
            leader[a]=b;
        }
        else
        {
            leader[b]=a;
            rank[a]++;
        }
    }

    int retans()
    {
        int ans=0;
        for(int i=0;i<leader.size();i++)
            if(leader[i]==i&&rank[i]>1) ans++;

        return ans-1;
    }
};



void solve()
{
    int h,w;
    cin>>h>>w;

    string str[h];

    for(int i=0;i<h;i++)
    {
        cin>>str[i];
    }

    str[0][0]=str[h-1][0]=str[h-1][w-1]=str[0][w-1]='#';

    djsets d;
    d.init(h+w);

    for(int i=0;i<h;i++)
    {
        for(int j=0;j<w;j++)
        {
            if(str[i][j]=='#')
            {
                d.connect(i,h+j);
            }
        }
    }

    int rows=0,col=0;

    for(int i=0;i<h;i++)
    {
        if(d.rank[d.retleader(i)]==1) rows++;
    }
    for(int i=h;i<h+w;i++)
    {
        if(d.rank[d.retleader(i)]==1) col++;
    }
    cout<<d.retans()+min(rows,col);

}
 
int32_t main()
{
    fastIO();
    //w(t)
    {
        solve();
        cout<<endl;
    }
    return 0;
}
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <=b ; ++i)
#define per(i, b, a) for(int i = b; i >= a; --i)
#define ll long long
#define pb push_back
using namespace std;
inline void rd(int &x){scanf("%d", &x);}
inline void rd(ll &x){scanf("%lld", &x);}
int n, m, k;
const int maxn = 1145;
int par[maxn << 1];
int findroot(int u)
{
    return par[u] == u ? par[u] : (par[u] = findroot(par[u]));
}
void merge(int u, int v)
{
    if(findroot(u) == findroot(v)) return;
    par[findroot(u)] = v;
}
int main()
{
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m;
    rep(i, 1, n + m) par[i] = i;
    rep(i, 1, n)
    {
        string s;
        cin >> s;
        rep(j, 1, m) if(s[j - 1] == '#') merge(i, n + j);
    }
    merge(1, n + 1), merge(1, n + m), merge(n, n + 1), merge(n, n + m);
    int ans = 0;
    unordered_set<int> r, c;
    rep(i, 1, n) r.insert(findroot(i));
    rep(j, 1, m) c.insert(findroot(j + n));
    cout << min(r.size(), c.size()) - 1;
}