#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip> 
#include <stdio.h>
//eolibraries
#define lnf 3999999999999999999
#define inf 999999999
#define PI 3.14159265359
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define mkp(a,b) make_pair(a,b)
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define rsz(a,n) a.resize(n)
#define pii pair <int,int>
#define rep(i,n) for(int i = 0 ; i < n ; i++) 
#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(int i = x ; i < n ; i++)
#define vi vector <int> 
#define vivi vector <vi>
#define v(s) vector<s>
#define rsz(a,n) a.resize(n)
#define rszv(a,n,v) a.resize(n,v)
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//eodefine

const int max_n = 1005;

using namespace std;
int h,w;
bool usd[max_n][max_n],hv[4][max_n],a[max_n][max_n];
int next[4][max_n][max_n];

void read() {
	cin >> h >> w;

	rep(i,h){
		string s;
		cin >> s; 
		rep(j,sz(s)) {
			if(s[j]=='#') {
				a[i][j]=1;
			}	
		}
	}
}

void dfs(int x,int y,int type) {
	if(usd[x][y]) return;
	usd[x][y]=1;
	
	rep(i,h) {
		if(!a[i][y] or usd[i][y])continue;
		dfs(i,y,type);
		break;
	}
	
	rep(i,w) {
		if(!a[x][i] or usd[x][i])continue;
		dfs(x,i,type);
		break;
	}
	
	hv[0][y]=1;
	hv[1][x]=1;

}

int slv(int type = 0) {
	rep(i,h){
		rep(j,w)usd[i][j]=0;
	}
	
	rep(i,max(h,w)){rep(t,2)hv[t][i]=0;}

	rep(i,w) {
		if(a[0][i])dfs(0,i,type);
		if(a[h-1][i])dfs(h-1,i,type);
	}

	rep(i,h) {
		if(a[i][0])dfs(i,0,type);
		if(a[i][w-1])dfs(i,w-1,type);
	}
	
	int c=0;

	rep(i,h) {
		rep(j,w) {
			if(a[i][j] == 1 and usd[i][j]==0) {
				dfs(i,j,type);
				c++;
			}
		}
	}

	int e=0,r=0;
	crep(i,1,h-1) {
		if(hv[1][i]==0) e++;
	}
	crep(i,1,w-1) {
		if(hv[0][i]==0) r++;
	}
	return c+min(e,r);
}
int main(){
fcin;
	read();

	int ans = slv();
	
	cout << ans << "\n";

/*



*/

    return 0;   
}
//#define _GLIBCXX_DEBUG
//#include "atcoder/all"
//using namespace atcoder;
#include <bits/stdc++.h>
#define int long long
#define ll long long
using ull = unsigned long long;
using namespace std;
#define Dump(x)                             \
    if (dbg) {                              \
        cerr << #x << " = " << (x) << endl; \
    }
#define overload4(_1, _2, _3, _4, name, ...) name
#define FOR1(n) for (ll i = 0; i < (n); ++i)
#define FOR2(i, n) for (ll i = 0; i < (n); ++i)
#define FOR3(i, a, b) for (ll i = (a); i < (b); ++i)
#define FOR4(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define bit(n, k) (((n) >> (k)) & 1) /*nのk bit目*/
namespace mydef {
const int INF = 1ll << 60;
const int MOD = 1e9 + 7;
template <class T>
bool chmin(T& a, const T& b) {
    if (a > b) {
        a = b;
        return 1;
    } else
        return 0;
}
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    } else
        return 0;
}
void Yes(bool flag = true) {
    if (flag)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
void No(bool flag = true) {
    Yes(!flag);
}
void YES(bool flag = true) {
    if (flag)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}
void NO(bool flag = true) {
    YES(!flag);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T& val) {
    std::fill((T*)array, (T*)(array + N), val);
}
bool dbg = true;
}  // namespace mydef
using namespace mydef;
#define pb push_back
//#define mp make_pair
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int, int>>
#define vvp vector<vector<pair<int, int>>>
#define pi pair<int, int>
//#define P pair<int, int>
//#define V vector<int>
//#define S set<int>
#define asn ans

//UnionFind
//How to use
//unite(x,y) : 集合 x と y を併合する. 併合済のとき false, 未併合のとき true が返される
//find(k) : 要素 k が属する集合を求める
//size(k) : 要素 k が属する集合の要素の数を求める
struct UnionFind {
    vector<int> data;

    UnionFind(int sz) {
        data.assign(sz, -1);
    }

    bool unite(int x, int y) {
        x = find(x), y = find(y);
        if (x == y)
            return (false);
        if (data[x] > data[y])
            swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return (true);
    }

    int find(int k) {
        if (data[k] < 0)
            return (k);
        return (data[k] = find(data[k]));
    }

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

    int size(int k) {
        return (-data[find(k)]);
    }
};

int H, W;
string S[1010];
//vvi V[1010];

void solve() {
    UnionFind uf(H + W);
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (S[i][j] == 1) {
                uf.unite(i, j + H);
            }
        }
    }
    uf.unite(0, H - 1);
    uf.unite(0, H);
    uf.unite(0, H + W - 1);

    set<int> set_r, set_c;
    for (int i = 0; i < H; i++) {
        set_r.insert(uf.find(i));
    }
    for (int j = 0; j < W; j++) {
        set_c.insert(uf.find(j + H));
    }


    cout << min(set_r.size() - 1, set_c.size() - 1) << endl;
}

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    cin >> H >> W;
    for (int i = 0; i < H; i++) {
        cin >> S[i];
        for (auto& c : S[i]) {
            if (c == '.')
                c = 0;
            else
                c = 1;
        }
    }

    solve();
    return 0;
}
