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

#define REP(i,a,b) for(int i=a;i<=b;i++)
using LL = long long;

const int Nmax=2009;
int H,W,r[Nmax],c[Nmax],cnt,ans;
string S;
vector <int> adj[Nmax];

void Conn(int a, int b) {
    adj[a].push_back(b);    adj[b].push_back(a);
}

void DFS_r(int curr, int prev) {
    r[curr]=cnt;
    // cout<<curr<<endl;       getchar();
    for (auto next : adj[curr]) {
        if (next==prev) continue;
        if (!r[next]) DFS_r(next,curr);
    }
}

void DFS_c(int curr, int prev) {
    c[curr]=cnt;
    for (auto next : adj[curr]) {
        if (next==prev) continue;
        if (!c[next])DFS_c(next,curr);
    }
}

int main() {
    ios_base::sync_with_stdio(0);    cin.tie(0);
    cin>>H>>W;
    Conn(1,H+1);    Conn(1,H+W);
    Conn(H,H+1);    Conn(H,H+W);
    REP(i,1,H) {
        cin>>S;
        REP(j,1,W) {
            if (S[j-1]=='#') Conn(i,H+j);
        }
    }
    // cout<<"OK"<<endl;
    REP(i,1,H) {
        if (!r[i]) {
            ++cnt;      DFS_r(i,0);
        }
    }
    // cout<<"OK2"<<endl;
    ans=cnt-1;  cnt=0;
    REP(j,1,W) {
        if (!c[H+j]) {
            ++cnt;      DFS_c(H+j,0);
        }
    }
    ans=min(ans,cnt-1);
    cout<<ans;
    return 0;
}
#define George_Plover
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <algorithm>
#define MAXN 1100
#define LL long long
using namespace std;
int n,m;
bool visr[MAXN],visc[MAXN];
bool vis[MAXN][MAXN];
char s[MAXN][MAXN];
int fa[MAXN*MAXN];
queue<pair<int,int> >q;
int num(int x,int y)
{
    return (x-1)*m+y;
}
int find(int x)
{
    return fa[x]==x?x:fa[x]=find(fa[x]);
}
void BFS(int x,int y)
{
    vis[x][y]=1;
    q.push(make_pair(x,y));
    while(!q.empty())
    {
        int ux=q.front().first;
        int uy=q.front().second;
        q.pop();
        
        if(s[ux][1]=='#'||s[ux][m]=='#'||s[1][uy]=='#'||s[n][uy]=='#')
        {
            fa[find(num(ux,uy))]=0;
        }
        for(int j=2;j<m && !visr[ux];j++)
        {
            if(j!=uy && s[ux][j]=='#' && !vis[ux][j])
            {
                q.push(make_pair(ux,j));
                fa[find(num(ux,j))]=find(num(ux,uy));
                vis[ux][j]=1;
            }
        }
        for(int i=2;i<n && !visc[uy];i++)
        {
            if(i!=ux && s[i][uy]=='#' && !vis[i][uy])
            {
                q.push(make_pair(i,uy));
                fa[find(num(i,uy))]=find(num(ux,uy));
                vis[i][uy]=1;
            }
        }
        visr[ux]=visc[uy]=1;
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    
    for(int i=1;i<=n;i++)
        scanf("%s",s[i]+1);
    
    for(int i=1;i<=n*m;i++)fa[i]=i;
    
    if(n==2||m==2)
    {
        cout<<0<<endl;
        return 0;
    }
    int ans=0;
    
    for(int i=2;i<n;i++)
        for(int j=2;j<m;j++)
        {
            if(s[i][j]=='#' && !vis[i][j])
            {
                BFS(i,j);
                if(find(num(i,j)))
                {
                    //cout<<i<<" "<<j<<endl;
                    ans++;
                }
            }
        }
    
    for(int i=2;i<n;i++)
        if(s[i][1]=='#'||s[i][m]=='#')
            visr[i]=1;
    for(int j=2;j<m;j++)
        if(s[1][j]=='#'||s[n][j]=='#')
            visc[j]=1;
    
    
    int ansr=0,ansc=0;
    for(int i=2;i<n;i++)
        if(!visr[i])
            ansr++;
    
    for(int j=2;j<m;j++)
        if(!visc[j])
            ansc++;
    //cout<<ans<<endl;
    printf("%d\n",min(ansc,ansr)+ans);
    
    
    
    return 0;
}
