#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll MXN = 3e3 + 10;
ll H, W, Par[MXN], Sz[MXN];
vector<ll> R[MXN], C[MXN];
string s[MXN];
ll f1, f2, f3;
ll Find(ll x){
    return (x == Par[x] ? x : Par[x] = Find(Par[x]));
}
bool Union(ll u, ll v){
    u = Find(u), v = Find(v);
    if(u == v) return 0;
    if(Sz[v] > Sz[u]) swap(u, v);
    Sz[u] += Sz[v], Par[v] = u;
    return 1;
}
void Build(){
    for(int i = 1; i <= H + W; i ++){
        Par[i] = i, Sz[i] = 1;
    }
    for(int i = 1; i <= H; i ++){
        for(int j = 1; j <= W; j ++){
            if(s[i][j] == '#') Union(i, j + H);
        }
    }
}
int main(){
    //ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    cin >> H >> W;
    if(min(H, W) <= 2) return cout << 0, 0;
    for(int i = 1; i <= H; i ++){
        cin >> s[i];
        s[i] = "$" + s[i];
    }
    s[1][1] = s[1][W] = s[H][1] = s[H][W] = '#';
    Build();
    for(int i = 1; i <= H + W; i ++){
        if(Find(i) != i) continue;
        f1 += Union(i, 1);
    }


    Build();
    for(int i = 1; i <= H; i ++){
        f2 += Union(1, i);
    }



    Build();
    for(int j = 1; j <= W; j ++){
        f3 += Union(1, j + H);
    }
    for(int i = 1; i <= 100; i ++) Build();
    cout << min({f1, f2, f3}) << '\n';
    return 0;
}

#include<bits/stdc++.h>
#define MAX 1000000007
#define MOD 998244353
#define ll long long int
#define fo(i,a,n) for(int i=a;i<n;i++)
#define ull unsigned long long int
#define pb push_back
#define eb emplace_back
#define in insert
#define cl clear
#define ld long double
#define mp make_pair
#define ii pair<int,int>
const ll inf = 10e16+7;
using namespace std;
typedef vector<vector<int>> vvi;

void print(int arr[], int n){
    fo(i,1,n+1){
        cout<<arr[i]<<" "; 
    }
    cout<<endl;
}


int gc(int arr[], int n) {
    int high = 0;
    for (int i = 0; i < n; i++)
        high = max(high, arr[i]);
 
    int count[high + 1] = {0};
    for (int i = 0; i < n; i++)
        count[arr[i]]++;
 
    int counter = 0;
 
    for (int i = high; i >= 1; i--) {
        int j = i;
        counter = 0;
        while (j <= high) {
            if(count[j] >=2)
               return j;
           else if (count[j] == 1)        
                counter++;        
            j += i;
            if (counter == 2)        
                return i;
        }
    }
}


int main(){
 
    ios_base::sync_with_stdio(false);
    cin.tie(0);
 
    // int t;
    // cin>>t;
    // while(t--){
    // }

    int a,b;
    cin>>a>>b;

    int arr[b-a+1];
    int n=a;
    fo(i,0,b-a+1){
        arr[i]=n++;
    }

    cout<<gc(arr,b-a+1);
 
    return 0;
}