#include<bits/stdc++.h>
using namespace std;
const int N=1005;
char a[N][N];
int n,m;
vector<int>g[N*2];
bool vis[N*2];
void add(int u,int v){
        g[u].push_back(v);
        g[v].push_back(u);
}
void dfs(int u){
        vis[u]=1;
        for(auto v:g[u])if(!vis[v])dfs(v);
}
int main(){
        cin>>n>>m;
        for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)cin>>a[i][j];
        add(1,n),add(1,n+1),add(1,n+m);
        for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)if(a[i][j]=='#')add(i,n+j);
        int ans=0;
        for(int i=1;i<=n;i++)if(!vis[i])dfs(i),ans++;
        ans--;
        memset(vis,0,sizeof(vis));
        int now=0;
        for(int i=n+1;i<=n+m;i++)if(!vis[i])dfs(i),now++;
        now--;
        cout<<min(ans,now)<<endl;
        return 0;
}
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <iomanip>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
 
#define debug(x) cout << #x << " = " << x << endl
#define fori(i, ini, lim) for(int i = int(ini); i < int(lim); i++)
#define ford(i, ini, lim) for(int i = int(ini); i >= int(lim); i--)
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
 
const int MAX = 1000 + 5;
char grid[MAX][MAX], grid_cpy[MAX][MAX];
bool line_unblocked[MAX], column_unblocked[MAX];
int n, m;

void unblock(int x, int y);

void unblock_line(int x) {
    if (line_unblocked[x]) return;
    line_unblocked[x] = true;
    fori (i, 1, m + 1) {
        if (grid[x][i] == '#') unblock(x, i);
    }
}

void unblock_column(int y) {
    if (column_unblocked[y]) return;
    column_unblocked[y] = true;
    fori (i, 1, n + 1) {
        if (grid[i][y] == '#') unblock(i, y);
    }
}

void unblock(int x, int y) { 
    unblock_line(x);
    unblock_column(y);
}

void solve() {
    cin >> n >> m;
    fori (i, 1, n + 1) fori (j, 1, m + 1) cin >> grid[i][j];

    grid[1][1] = grid[1][m] = grid[n][1] = grid[n][m] = '#';
    memcpy(grid_cpy, grid, sizeof(char) * MAX * MAX);

    int line_cost = 0;
    unblock(1, 1);
    fori (i, 1, n + 1) {
        if (!line_unblocked[i]) {
            grid[i][1] = '#';
            line_cost++;
            unblock(i, 1);
        }
    }

    memcpy(grid, grid_cpy, sizeof(char) * MAX * MAX);
    memset(line_unblocked, false, sizeof line_unblocked);
    memset(column_unblocked, false, sizeof column_unblocked);

    int column_cost = 0;
    unblock(1, 1);
    fori (i, 1, m + 1) {
        if (!column_unblocked[i]) {
            grid[1][i] = '#';
            column_cost++;
            unblock(1, i);
        }
    }

    cout << min(line_cost, column_cost) << endl;
}
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    solve();
    return 0;
}
