#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PP;
#define MOD 1000000007
//#define MOD 998244353
#define INF 2305843009213693951
//#define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i,n) for(ll i=0;i<(n);++i)
#define OREP(i,n) for(ll i=1;i<=(n);++i)
#define RREP(i,n) for(ll i=(n)-1;i>=0;--i)
#define all1(i) begin(i),end(i)
#define GOODBYE do { cout << "-1" << endl; return 0; } while (false)
#define MM <<" "<<
#define Endl endl
#define debug true
#define debug2 false

class unionfind{
    /*
    Copyright (c) 2020 0214sh7
    https://github.com/0214sh7/library/
    */
    private:
    std::vector<int> UF,rank;
    public:
    
    void init(int N){
        UF.clear();
        rank.clear();
        for(int i=0;i<N;i++){
            UF.push_back(i);
            rank.push_back(0);
        }
    }
    
    int root(int k){
        if(UF[k]==k){
            return k;
        }else{
            UF[k]=root(UF[k]);
            return UF[k];
        }
    }
    
    bool same(int p,int q){
        return root(p)==root(q);
    }
    
    void unite(int P,int Q){
        int p=root(P);
        int q=root(Q);
        if(p==q)return;
        if(rank[p]<rank[q])std::swap(p,q);
        UF[q]=p;
        if(rank[p]==rank[q])rank[p]++;
    }
    
};


int main(void){
    ll H,W;
    vector<string> S;
    cin >> H >> W;
    REP(i,H){
        string s;
        cin >> s;
        S.push_back(s);
    }
    
    if(H<=2 || W<=2){
        cout << 0 << endl;
        return 0;
    }
    
    unionfind UF;
    UF.init(H+W-3);
    
    //0:外周
    //1~H-2:横線
    //H-1~H+W-4:縦線
    
    for(ll i=1;i<H-1;i++){
        if(S[i][0]=='#' || S[i][W-1]=='#'){
            UF.unite(0,i);
        }
    }
    
    for(ll j=1;j<W-1;j++){
        if(S[0][j]=='#' || S[H-1][j]=='#'){
            UF.unite(0,H-2+j);
        }
    }
    
    for(ll i=1;i<H-1;i++){
        for(ll j=1;j<W-1;j++){
            if(S[i][j]=='#'){
                UF.unite(i,H-2+j);
            }
        }
    }
    
    set<ll> Y,X;
    Y.insert(UF.root(0));
    X.insert(UF.root(0));
    for(ll i=1;i<H-1;i++){
        Y.insert(UF.root(i));
    }
    for(ll i=H-1;i<H+W-3;i++){
        X.insert(UF.root(i));
    }
    
    ll R=Y.size()-1,C=X.size()-1;
    
    //cout << R MM C << endl;
    cout << min(R,C) << endl;
    
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;

#ifndef ONLINE_JUDGE
#define F_AND_L	__FUNCTION__ << ":" << __LINE__
#define X(x) { cerr << F_AND_L << "[ " << #x << " : " << x << " ]" << endl;}
#define D(x) << "[ " << #x << " : " << x << " ] "
#define S(...) cerr << F_AND_L __VA_ARGS__ << endl;
#define PC(a) { cerr << F_AND_L << " " << #a << "=["; for(auto __xmmp: a) { cerr << __xmmp << " "; } cerr << "]" << endl; }   
#define PA(a,s,l) { cerr << F_AND_L << " " << #a << "=["; for(int __xmmp=s; __xmmp<s+l; __xmmp++) { cerr << a[__xmmp] << " "; } cerr << "]" << endl; }
#else
#define X(x)
#define D(x)
#define S(...)
#define PC(a)
#define PA(a,s,l)
#endif

#define rep(i, s, e) for(int i=s; i<e; i++)
#define reps(i, s, e, stp) for(int i=s; i<e; i+=stp)
#define per(i, s, e) for(int i=e; i>=0; i--)
#define pers(i, s, e, stp) for(int i=e; i>=0; i+=stp)
#define F(i, n) for(int i=0; i<n; i++)
#define R(i, n) for(int i=n-1; i>=0; i--)

int main() {
	
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	int h, w, m=0;
	cin >> h >> w;

	int a[h][w];
	for(int i=0; i<h; i++) {
		for(int j=0; j<w; j++) {
			cin >> a[i][j];
			if(i == 0 && j == 0)
				m = a[i][j];
			m=min(a[i][j], m);
		}
	}
	ll ans=0;
	for(int i=0; i<h; i++) {
		for(int j=0; j<w; j++) {
			ans += a[i][j] - m;
		}
	}
	cout << ans << endl;
}
