#include <bits/stdc++.h>
#define s second
#define f first
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define read(x) for(auto& qw : (x)) cin >> qw;
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MAX = 2e3 + 10;

/*
row: i
col: N + j
*/
vector<int> g[MAX];
vector<int> vis(MAX, 0);
int N,M;
char A[MAX][MAX];

void print(int v){
	if(v < N){
		cout << "row " << v;
	}
	else cout << "col " << v-N;
}

void dfs(int v){
	vis[v] = 1;
	for(int u : g[v]){
		if(!vis[u]){
			/*print(v);
			cout << " ";
			print(u);
			cout << endl;*/

			dfs(u);
		}
	}
	
}

void test_case(){

	cin >> N >> M;

	for(int i = 0; i < N; i++){
		for(int j = 0; j < M; j++){
			cin >> A[i][j];

			if(A[i][j] == '#'){
				g[i].pb(N+j);
				g[N+j].pb(i);
			}
		}
	}

	int ans = 0;
	dfs(0);
	dfs(N-1);
	dfs(N);
	dfs(N+M-1);

	for(int i = 1; i < N; i++){
		if(!vis[i]){
			ans++;
			dfs(i);
		}
	}

	int ans1 = 0;
	vis.assign(MAX,0);
	
	dfs(0);
	dfs(N-1);
	dfs(N);
	dfs(N+M-1);

	for(int i = N+1; i < N+M; i++){
		if(!vis[i]){
			ans1++;
			dfs(i);
		}
	}

	cout << min(ans, ans1) << endl;
}

int main(){ _

	int tt = 1;
//	cin >> tt;

	while(tt--){
		test_case();
	}

	return 0;
}

//#include <atcoder/maxflow.hpp>
#include <memory>
#include <iostream>
#include <map>
#include <list>
#include <set>
#include <algorithm>
#include <vector>
#include <sstream>
#include <string>
#include <functional>
#include <queue>
#include <deque>
#include <stack>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <fstream>
#include <iterator>
#include <random>
#include <chrono>
#include <complex>
#include <thread>
#include <bitset>
#include <assert.h>

#define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i) int i; cin >> i
#define readll(i) ll i; cin >> i
#define readdouble(i) double i; cin >> i
#define readstring(s) string s; cin >> s

typedef long long ll;

using namespace std;
//using namespace atcoder;

//ll modd = (1000LL * 1000LL * 1000LL + 7LL);
ll modd = 998244353;

class UnionFind {
    public:
      vector<int> parent;
      vector<int> size;
//      size[i] = number of nodes in tree rooted at i
// Note: not necessarily correct if i is not a root node
      set<int> roots;

    UnionFind(int n) : parent(n), size(n) {
        forr(i,0,n) { roots.insert(i); }
        forr(i,0,n) {
            parent[i] = i; size[i] = 1;
        }
    } 

    int Find(int p) {
        int root = p;
        while (root != parent[root]) {  root = parent[root]; }
        while (p != root) {
            int newp = parent[p];
            parent[p] = root;
            p = newp;
        }
        return root;
    }

    void Union(int p, int q) {
        int rootP = Find(p);
        int rootQ = Find(q);
        if (rootP == rootQ) return;

        if (size[rootP] < size[rootQ]) {
            parent[rootP] = rootQ;
            size[rootQ] += size[rootP];
            roots.erase(rootP);
        }
        else {
            parent[rootQ] = rootP;
            size[rootP] += size[rootQ];
            roots.erase(rootQ);
        }
    }

};


int main(int argc, char *argv[])   {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cout.precision(17);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<int> rand_gen(0, modd);   // rand_gen(rng) gets the rand no

//    auto start = chrono::steady_clock::now();

//    readint(test_cases);
    int test_cases = 1;
    forr(t, 1, test_cases) {
        readint(h); readint(w);
        vector<string> grid;
        forr(i,0,h) {
            readstring(s);
            grid.push_back(s);
        }

        UnionFind uf(h+w);
        uf.Union(0, h+0);
        uf.Union(h-1, h+0);
        uf.Union(0, h+w-1);
        uf.Union(h-1, h+w-1);
        forr(i,0,h) {
            forr(j,0,w) {
                if (grid[i][j]=='#') {
                    uf.Union(i, h+j);
                }
            }
        }
        UnionFind ufh(h);
        forr(i,0,h) {
            forr(j,0,h) {
                if (uf.Find(i)==uf.Find(j)) {
                    ufh.Union(i,j);
                }
            }
        }
        UnionFind ufv(w);
        forr(i,0,w) {
            forr(j,0,w) {
                if (uf.Find(h+i)==uf.Find(h+j)) {
                    ufv.Union(i,j);
                }
            }
        }

        cout << min(ufv.roots.size()-1, ufh.roots.size()-1) << endl;        

    }

//        auto stop = chrono::steady_clock::now(); 
//        auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
//        cout << "Duration: " << duration.count() << endl;
    return 0;
}
