#include <cmath>
#include <climits>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <complex>
#include <algorithm>
#include <cstring>
#include <cassert>
using namespace std;;
#define end(c) (c).end()
#define bgn(c) (c).begin()
#define all(c) bgn(c),end(c)
#define sz(c) int((c).size())
#define bck(c) c.back()
#define rep(i,n) for(int i=0, _##i=(n); i<_##i; ++i)
#define dwn(i,n) for(int i=(n); i-->0; )
#define repr(i,l,r) for(int i=(l), _##i=(r); i<_##i; ++i)
#define dwnr(i,r,l) for(int i=(r), _##i=(l); i-->_##i; )
#define repi(i,a) for(auto i=bgn(a), _##i=end(a); i!=_##i; ++i)
#define dwni(i,a) for(auto i=(a).rbegin(), _##i=(a).rend(); i!=_##i; ++i)
#define fore(i,a) for(auto i:a)
#define smin(a,b) a=min(a,b)
#define smax(a,b) a=max(a,b)
#define inf INT_MAX
#define eps 1e-9
#define pi 3.1415926535897932384626
#define vec vector
#define fila(a,b,v) fill(a,a+asz(a,b),v)
#define fil(a,v) fill(all(a),v)
#define pb push_back
#define popb pop_back
#define popf pop_front
#define mp make_pair
#define zro(c) memset(c,0,sizeof(c))
#define neg(c) memset(c,-1,sizeof(c))
#define ord(a,b) do{if(a>b)swap(a,b);}while(0)
#define at(c,i) ((c>>i)&1)
#define dbg(a...) do{cerr<<#a<<" = ";_d,a;cerr<<endl;}while(0)
#define dbga(a, n)do{rep(_i,n)cout<<a[_i]<<' ';cout<<endl;}while(0)
#define big long long
#define dub double
#define pp pair
#define pii pp<int, int>
#define pbb pp<big, big>
#define pdd pp<dub, dub>
#define vi vec<int>
#define vb vec<big>
#define vpi vec<pii>
#define str string
#define bigd long double
#define ff first
#define ss second
#define pti complex<int>
#define ptb complex<big>
#define xx real
#define yy imag
#define mine(c...) min_element(c)
#define maxe(c...) max_element(c)
#define bin binary_search
#define lb lower_bound
#define ub upper_bound
#define msi map<string, int>
#define mii map<int, int>
#define cnt __builtin_popcount
#define gcd __gcd
#define pq priority_queue
#define pl(x) cout<<(x)<<'\n'
#define fin(x) do{pl(x);return;}while(0)
#define sqr(x) ((x)*(x))
#define in(l,x,r) ((l)<=(x)&&(x)<(r))
struct _dbg{
	template<class T> _dbg& operator,(const T& v){
		cerr<<v<<" ";
		return *this;};};
_dbg _d;
void go();
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);go();};

const int mN = 60;
char a[mN][mN];
int v[mN][mN], N, M, s;
int di[]{1,-1,0,0}, dj[]{0,0,1,-1};

int f(int i, int j){
	if(v[i][j]||!in(0,i,N)||!in(0,j,M)||a[i][j]=='.') return 0;
	int r=1; v[i][j] = 1;
	rep(k,4) r+=f(i+di[k], j+dj[k]);
	return r;};

int n(int x, int y){
	int r=0; zro(v), v[x][y] = 1;
	rep(i,N) rep(j,M) r += f(i,j)>0;
	return r;};

void go(){
	cin>>N>>M;rep(i,N)rep(j,M){
		cin>>a[i][j];
		s += a[i][j]=='#';};
	if(s<=2) fin(-1);
	rep(i,N)rep(j,M){
		dbg(i,j,n(i,j));
		if(n(i,j)>=2) fin(1);};
	fin(2);};


#include <cmath>
#include <climits>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <complex>
#include <algorithm>
#include <cstring>
#include <cassert>
using namespace std;;
#define end(c) (c).end()
#define bgn(c) (c).begin()
#define all(c) bgn(c),end(c)
#define sz(c) int((c).size())
#define bck(c) c.back()
#define rep(i,n) for(int i=0, _##i=(n); i<_##i; ++i)
#define dwn(i,n) for(int i=(n); i-->0; )
#define repr(i,l,r) for(int i=(l), _##i=(r); i<_##i; ++i)
#define dwnr(i,r,l) for(int i=(r), _##i=(l); i-->_##i; )
#define repi(i,a) for(auto i=bgn(a), _##i=end(a); i!=_##i; ++i)
#define dwni(i,a) for(auto i=(a).rbegin(), _##i=(a).rend(); i!=_##i; ++i)
#define fore(i,a) for(auto i:a)
#define smin(a,b) a=min(a,b)
#define smax(a,b) a=max(a,b)
#define inf INT_MAX
#define eps 1e-9
#define pi 3.1415926535897932384626
#define vec vector
#define fila(a,b,v) fill(a,a+asz(a,b),v)
#define fil(a,v) fill(all(a),v)
#define pb push_back
#define popb pop_back
#define popf pop_front
#define mp make_pair
#define zro(c) memset(c,0,sizeof(c))
#define neg(c) memset(c,-1,sizeof(c))
#define ord(a,b) do{if(a>b)swap(a,b);}while(0)
#define at(c,i) ((c>>i)&1)
#define dbg(a...) do{cerr<<#a<<" = ";_d,a;cerr<<endl;}while(0)
#define dbga(a, n)do{rep(_i,n)cout<<a[_i]<<' ';cout<<endl;}while(0)
#define big long long
#define dub double
#define pp pair
#define pii pp<int, int>
#define pbb pp<big, big>
#define pdd pp<dub, dub>
#define vi vec<int>
#define vb vec<big>
#define vpi vec<pii>
#define str string
#define bigd long double
#define ff first
#define ss second
#define pti complex<int>
#define ptb complex<big>
#define xx real
#define yy imag
#define mine(c...) min_element(c)
#define maxe(c...) max_element(c)
#define bin binary_search
#define lb lower_bound
#define ub upper_bound
#define msi map<string, int>
#define mii map<int, int>
#define cnt __builtin_popcount
#define gcd __gcd
#define pq priority_queue
#define pl(x) cout<<(x)<<'\n'
#define fin(x) do{pl(x);return;}while(0)
#define sqr(x) ((x)*(x))
#define in(l,x,r) ((l)<=(x)&&(x)<(r))
struct _dbg{
	template<class T> _dbg& operator,(const T& v){
		cerr<<v<<" ";
		return *this;};};
_dbg _d;
void go();
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);go();};

const int mN = 60;
char a[mN][mN];
int v[mN][mN], N, M, r=inf;
int dx[]{0, -1, 0, 1}, dy[]{1, 0, -1, 0};

int f(int x, int y, int i, int j){
	if(!in(0,x,N)||!in(0,y,M)||v[x][y]||a[x][y]=='.') return 0;
	if(x==i&&y==j) return 1;
	int s=0; v[x][y]=1;
	rep(k,4) s+=f(x+dx[k],y+dy[k],i,j);
	return s;};

void go(){
	cin>>N>>M;rep(i,N)cin>>a[i];
	rep(i,N)rep(j,M)if(a[i][j]=='#'){
		rep(x,N)rep(y,M)if(a[x][y]=='#'&&!(i==x&&j==y)){
			smin(r,f(i,j,x,y));
			zro(v);};
		fin(r);};};


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
using namespace std;

#define task "a"

#define fi first
#define se second
#define mp make_pair
typedef long long ll;

const ll MOD = 1e9 + 7;
const int INF = (int)1e9;

vector<vector<int> > a;
vector<vector<int> > b;
int n, m;

void dfs(int i, int j) {
    if(!((0 <= i && i < n) && (0 <= j && j < m)) || !b[i][j]) {
        return;
    }
    b[i][j] = 0;
    dfs(i + 1, j);
    dfs(i, j + 1);
    dfs(i - 1, j);
    dfs(i, j - 1);
}

int check() {
    int cnt = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (b[i][j]) {
                cnt++;
                dfs(i, j);
            }
        }
    }
    return (cnt >= 2);
}

int main() {
   // freopen(task".in", "r", stdin);
   // freopen(task".out", "w", stdout);
    cin >> n >> m;
    int c = 0;
    a = vector<vector<int> > (n, vector<int>(m, 0));
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        for (int j = 0; j < m; j++) {
            if (s[j] == '#') {
                c++;
                a[i][j] = 1;
            }
        }
    }

    b = a;
    if(check()) {
        cout << "0" << endl;
        return 0;
    }

    if (c <= 2) {
        cout << -1 << endl;
        return 0;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            b = a;
            b[i][j] = 0;
            if (check()) {
                cout << 1 << endl;
                return 0;
            }
        }
    }
    cout << 2 << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>

#define FOR(i,b,e) for(int i=(b); i <= (e); ++i)
#define FORD(i,b,e) for(int i=(b); i >= (e); --i)
#define SIZE(c) (int) (c).size()
#define FORE(i,c) FOR(i,0,SIZE(c)-1)
#define FORDE(i,c) FORD(i,SIZE(c)-1,0)
#define MIN(x,y) ( ((x) < (y))? (x) : (y) )
#define MAX(x,y) ( ((x) > (y))? (x) : (y) )
#define PB push_back
#define MP make_pair
#define ST first
#define ND second
#define INF 1000000001

using namespace std;

typedef pair < int , int > PII;
typedef long long int LLI;

typedef vector < int > VI;
typedef vector < bool > VB;
typedef vector < PII > VP;
typedef vector < LLI > VL;

typedef vector < VI > VVI;
typedef vector < VL > VVL;
typedef vector < VB > VVB;

struct FAU
{
    vector < int > p; //parent
    vector < int > r; //rank

    FAU(int n)
    {
        p.resize(n,-1);
        r.resize(n,0);
    }

    int Find(int x)
    {
        if (p[x] == -1) return x;

        p[x] = Find(p[x]);
        return p[x];
    }

    bool Together(int x, int y)
    {
        return (Find(x) == Find(y));
    }

    void Union(int x, int y)
    {
        x = Find(x);
        y = Find(y);

        if (x == y) return ;

        if (r[x] > r[y]) p[y] = x;
        else p[x] = y;

        if (r[x] == r[y]) ++r[y];

        return ;
    }
};

bool isConnected(VVB &T)
{
    int n = SIZE(T),
        m = SIZE(T[0]);

    FAU F(n * m);

    FOR(i,0,n-1) FOR(j,0,m-1)
        if (T[i][j])
        {
            if (i < n-1 && T[i+1][j])
                F.Union(i * m + j, (i + 1) * m + j);

            if (j < m-1 && T[i][j+1])
                F.Union(i * m + j, i * m + j + 1);
        }

    int tmp = -1;

    FOR(i,0,n-1) FOR(j,0,m-1)
        if (T[i][j])
        {
            if (tmp == -1)
                tmp = F.Find(i * m + j);
            else if (tmp != F.Find(i * m + j))
                return 0;
        }

    return 1;
}

/*************************************************************************/

int main()
{
    ios_base::sync_with_stdio(0);

    int n, m;
    cin >> n >> m;

    VVB T(n);
    FORE(i,T) T[i].resize(m);

    FOR(i,0,n-1) FOR(j,0,m-1)
    {
        char c;
        cin >> c;

        T[i][j] = (c == '#');
    }

    FOR(i,0,n-1) FOR(j,0,m-1)
        if (T[i][j])
        {
            T[i][j] = 0;

            if (!isConnected(T))
            {
                cout << 1;
                return 0;
            }

            T[i][j] = 1;
        }

    cout << 2;

    return 0;
}

/*************************************************************************/


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>

#define FOR(i,b,e) for(int i=(b); i <= (e); ++i)
#define FORD(i,b,e) for(int i=(b); i >= (e); --i)
#define SIZE(c) (int) (c).size()
#define FORE(i,c) FOR(i,0,SIZE(c)-1)
#define FORDE(i,c) FORD(i,SIZE(c)-1,0)
#define MIN(x,y) ( ((x) < (y))? (x) : (y) )
#define MAX(x,y) ( ((x) > (y))? (x) : (y) )
#define PB push_back
#define MP make_pair
#define ST first
#define ND second
#define INF 1000000001

using namespace std;

typedef pair < int , int > PII;
typedef long long int LLI;

typedef vector < int > VI;
typedef vector < bool > VB;
typedef vector < PII > VP;
typedef vector < LLI > VL;

typedef vector < VI > VVI;
typedef vector < VL > VVL;
typedef vector < VB > VVB;

struct FAU
{
    vector < int > p; //parent
    vector < int > r; //rank

    FAU(int n)
    {
        p.resize(n,-1);
        r.resize(n,0);
    }

    int Find(int x)
    {
        if (p[x] == -1) return x;

        p[x] = Find(p[x]);
        return p[x];
    }

    bool Together(int x, int y)
    {
        return (Find(x) == Find(y));
    }

    void Union(int x, int y)
    {
        x = Find(x);
        y = Find(y);

        if (x == y) return ;

        if (r[x] > r[y]) p[y] = x;
        else p[x] = y;

        if (r[x] == r[y]) ++r[y];

        return ;
    }
};

bool isConnected(VVB &T)
{
    int n = SIZE(T),
        m = SIZE(T[0]);

    FAU F(n * m);

    FOR(i,0,n-1) FOR(j,0,m-1)
        if (T[i][j])
        {
            if (i < n-1 && T[i+1][j])
                F.Union(i * m + j, (i + 1) * m + j);

            if (j < m-1 && T[i][j+1])
                F.Union(i * m + j, i * m + j + 1);
        }

    int tmp = -1;

    FOR(i,0,n-1) FOR(j,0,m-1)
        if (T[i][j])
        {
            if (tmp == -1)
                tmp = F.Find(i * m + j);
            else if (tmp != F.Find(i * m + j))
                return 0;
        }

    return 1;
}

/*************************************************************************/

int main()
{
    ios_base::sync_with_stdio(0);

    int n, m;
    cin >> n >> m;

    VVB T(n);
    FORE(i,T) T[i].resize(m);

    int cnt = 0;

    FOR(i,0,n-1) FOR(j,0,m-1)
    {
        char c;
        cin >> c;

        T[i][j] = (c == '#');
        cnt += T[i][j];
    }

    if (cnt <= 2)
    {
        cout << -1;
        return 0;
    }

    FOR(i,0,n-1) FOR(j,0,m-1)
        if (T[i][j])
        {
            T[i][j] = 0;

            if (!isConnected(T))
            {
                cout << 1;
                return 0;
            }

            T[i][j] = 1;
        }

    cout << 2;

    return 0;
}

/*************************************************************************/


#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

struct Counter
{
	static int k;
	Counter() {k++;}
	~Counter() {k--;}
};

int Counter::k = 0;

template<typename T>
void pr(const string& name, T t)
{
    cout << name << " = " << t << endl;
}

template<typename T, typename ... Types>
void pr(const string& names, T t, Types ... rest)
{
    auto comma_pos = names.find(',');
    cout << names.substr(0, comma_pos) << " = " << t << ", ";

    auto next_name_pos = names.find_first_not_of(" \t\n", comma_pos + 1);
    pr(string(names, next_name_pos), rest ...);
}

void mod(ll &a, ll b)
{
    a %= b;
    if(a < 0) a += b;
}

#define all(x) x.begin(), x.end()
#define f(i,a,b) for(int i = (a); i <= (b); i++)
#define fd(i,a,b) for(int i = (a); i >= (b); i--)
#define mp make_pair
#define faster_io() ios_base::sync_with_stdio(false)
#define pb push_back
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
#define trace(...) pr(#__VA_ARGS__, __VA_ARGS__);
#define tracea(x) {string s = #x; Counter c##x; cout<<s.substr(0,1+s.find('['))<<Counter::k<<"]="<<x<<'\n';}
#define vii vector<pair<int,int>>

const ll MOD = 1000000007;

// ----------------------------------------------------------------------------------------------------------

bool V[55][55];
char B[55][55];
int N, M;

void dfs(int i, int j)
{
    if(B[i][j] != '#') return;
    if(V[i][j]) return;

    V[i][j] = true;

    dfs(i+1,j);
    dfs(i,j+1);
    dfs(i-1,j);
    dfs(i,j-1);
}

int main()
{
    cin >> N >> M;

    f(i,1,N)
    {
        string s;
        cin >> s;
        f(j,1,M) B[i][j] = s[j-1];
    }

    int tot = 0;

    f(i,1,N) f(j,1,N) tot += B[i][j] == '#';

    if(tot < 2)
    {
        cout << "-1";
        return 0;
    }

    int c = 0;
    f(i,1,N) f(j,1,N) if(B[i][j] == '#' && !V[i][j]) dfs(i,j), c++;
    if(c>1)
    {
        cout << "0";
        return 0;
    }

    f(i,1,N) f(j,1,N) if(B[i][j] == '#')
    {
        B[i][j] = '.';
        int comps = 0;

        f(x,1,N) f(y,1,N) V[x][y] = false;
        f(x,1,N) f(y,1,N) if(B[x][y] == '#' && !V[x][y])
        {
            comps++;
            dfs(x,y);
        }

        if(comps > 1)
        {
            cout << "1";
            return 0;
        }

        B[i][j] = '#';
    }

    cout << "2";
}


#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

struct Counter
{
	static int k;
	Counter() {k++;}
	~Counter() {k--;}
};

int Counter::k = 0;

template<typename T>
void pr(const string& name, T t)
{
    cout << name << " = " << t << endl;
}

template<typename T, typename ... Types>
void pr(const string& names, T t, Types ... rest)
{
    auto comma_pos = names.find(',');
    cout << names.substr(0, comma_pos) << " = " << t << ", ";

    auto next_name_pos = names.find_first_not_of(" \t\n", comma_pos + 1);
    pr(string(names, next_name_pos), rest ...);
}

void mod(ll &a, ll b)
{
    a %= b;
    if(a < 0) a += b;
}

#define all(x) x.begin(), x.end()
#define f(i,a,b) for(int i = (a); i <= (b); i++)
#define fd(i,a,b) for(int i = (a); i >= (b); i--)
#define mp make_pair
#define faster_io() ios_base::sync_with_stdio(false)
#define pb push_back
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
#define trace(...) pr(#__VA_ARGS__, __VA_ARGS__);
#define tracea(x) {string s = #x; Counter c##x; cout<<s.substr(0,1+s.find('['))<<Counter::k<<"]="<<x<<'\n';}
#define vii vector<pair<int,int>>

const ll MOD = 1000000007;

// ----------------------------------------------------------------------------------------------------------

char B[55][55];
int N, M;

int main()
{
    cin >> N >> M;

    f(i,1,N)
    {
        string s;
        cin >> s;
        f(j,1,M) B[i][j] = s[j-1];
    }

    int ans = 999999;

    f(i,1,N) f(j,1,N) if(B[i][j] == '#')
    {
        int moves = 0;
        f(x,i-1,i+1) f(y,j-1,j+1) moves += (B[x][y] == '#');
        ans = min(ans, moves-1);
    }

    cout << ans;
}


#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

struct Counter
{
	static int k;
	Counter() {k++;}
	~Counter() {k--;}
};

int Counter::k = 0;

template<typename T>
void pr(const string& name, T t)
{
    cout << name << " = " << t << endl;
}

template<typename T, typename ... Types>
void pr(const string& names, T t, Types ... rest)
{
    auto comma_pos = names.find(',');
    cout << names.substr(0, comma_pos) << " = " << t << ", ";

    auto next_name_pos = names.find_first_not_of(" \t\n", comma_pos + 1);
    pr(string(names, next_name_pos), rest ...);
}

void mod(ll &a, ll b)
{
    a %= b;
    if(a < 0) a += b;
}

#define all(x) x.begin(), x.end()
#define f(i,a,b) for(int i = (a); i <= (b); i++)
#define fd(i,a,b) for(int i = (a); i >= (b); i--)
#define mp make_pair
#define faster_io() ios_base::sync_with_stdio(false)
#define pb push_back
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
#define trace(...) pr(#__VA_ARGS__, __VA_ARGS__);
#define tracea(x) {string s = #x; Counter c##x; cout<<s.substr(0,1+s.find('['))<<Counter::k<<"]="<<x<<'\n';}
#define vii vector<pair<int,int>>

const ll MOD = 1000000007;

// ----------------------------------------------------------------------------------------------------------

char B[55][55];
int N, M;

int main()
{
    cin >> N >> M;

    f(i,1,N)
    {
        string s;
        cin >> s;
        f(j,1,M) B[i][j] = s[j-1];
    }

    int tot = 0;

    f(i,1,N) f(j,1,M) tot += B[i][j] == '#';

    if(tot < 2)
    {
        cout << "-1";
        return 0;
    }

    if(tot == 2)
    {
        f(i,1,N) f(j,1,M) if(B[i][j] == '#' && B[i+1][j] == '#')
        {
            cout << "-1";
            return 0;
        }
        f(i,1,N) f(j,1,M) if(B[i][j] == '#' && B[i][j+1] == '#')
        {
            cout << "-1";
            return 0;
        }
    }

    int ans = 999999;

    f(i,1,N) f(j,1,M) if(B[i][j] == '#')
    {
        int moves = 0;
        f(x,i-1,i+1) f(y,j-1,j+1) if(x==i || y==j) moves += (B[x][y] == '#');
        ans = min(ans, moves-1);
    }

    cout << ans;
}


#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

struct Counter
{
	static int k;
	Counter() {k++;}
	~Counter() {k--;}
};

int Counter::k = 0;

template<typename T>
void pr(const string& name, T t)
{
    cout << name << " = " << t << endl;
}

template<typename T, typename ... Types>
void pr(const string& names, T t, Types ... rest)
{
    auto comma_pos = names.find(',');
    cout << names.substr(0, comma_pos) << " = " << t << ", ";

    auto next_name_pos = names.find_first_not_of(" \t\n", comma_pos + 1);
    pr(string(names, next_name_pos), rest ...);
}

void mod(ll &a, ll b)
{
    a %= b;
    if(a < 0) a += b;
}

#define all(x) x.begin(), x.end()
#define f(i,a,b) for(int i = (a); i <= (b); i++)
#define fd(i,a,b) for(int i = (a); i >= (b); i--)
#define mp make_pair
#define faster_io() ios_base::sync_with_stdio(false)
#define pb push_back
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
#define trace(...) pr(#__VA_ARGS__, __VA_ARGS__);
#define tracea(x) {string s = #x; Counter c##x; cout<<s.substr(0,1+s.find('['))<<Counter::k<<"]="<<x<<'\n';}
#define vii vector<pair<int,int>>

const ll MOD = 1000000007;

// ----------------------------------------------------------------------------------------------------------

char B[55][55];
int N, M;

int main()
{
    cin >> N >> M;

    f(i,1,N)
    {
        string s;
        cin >> s;
        f(j,1,M) B[i][j] = s[j-1];
    }

    int tot = 0;

    f(i,1,N) f(j,1,N) tot += B[i][j] == '#';

    if(tot < 2)
    {
        cout << "-1";
        return 0;
    }

    if(tot == 2)
    {
        f(i,1,N) f(j,1,N) if(B[i][j] == '#' && B[i+1][j] == '#')
        {
            cout << "-1";
            return 0;
        }
        f(i,1,N) f(j,1,N) if(B[i][j] == '#' && B[i][j+1] == '#')
        {
            cout << "-1";
            return 0;
        }
    }

    int ans = 999999;

    f(i,1,N) f(j,1,N) if(B[i][j] == '#')
    {
        int moves = 0;
        f(x,i-1,i+1) f(y,j-1,j+1) if(x==i || y==j) moves += (B[x][y] == '#');
        ans = min(ans, moves-1);
    }

    cout << ans;
}


#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

struct Counter
{
	static int k;
	Counter() {k++;}
	~Counter() {k--;}
};

int Counter::k = 0;

template<typename T>
void pr(const string& name, T t)
{
    cout << name << " = " << t << endl;
}

template<typename T, typename ... Types>
void pr(const string& names, T t, Types ... rest)
{
    auto comma_pos = names.find(',');
    cout << names.substr(0, comma_pos) << " = " << t << ", ";

    auto next_name_pos = names.find_first_not_of(" \t\n", comma_pos + 1);
    pr(string(names, next_name_pos), rest ...);
}

void mod(ll &a, ll b)
{
    a %= b;
    if(a < 0) a += b;
}

#define all(x) x.begin(), x.end()
#define f(i,a,b) for(int i = (a); i <= (b); i++)
#define fd(i,a,b) for(int i = (a); i >= (b); i--)
#define mp make_pair
#define faster_io() ios_base::sync_with_stdio(false)
#define pb push_back
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
#define trace(...) pr(#__VA_ARGS__, __VA_ARGS__);
#define tracea(x) {string s = #x; Counter c##x; cout<<s.substr(0,1+s.find('['))<<Counter::k<<"]="<<x<<'\n';}
#define vii vector<pair<int,int>>

const ll MOD = 1000000007;

// ----------------------------------------------------------------------------------------------------------

bool V[55][55];
char B[55][55];
int N, M;

void dfs(int i, int j)
{
    if(B[i][j] != '#') return;
    if(V[i][j]) return;

    V[i][j] = true;

    dfs(i+1,j);
    dfs(i,j+1);
    dfs(i-1,j);
    dfs(i,j-1);
}

int main()
{
    cin >> N >> M;

    f(i,1,N)
    {
        string s;
        cin >> s;
        f(j,1,M) B[i][j] = s[j-1];
    }

    int tot = 0;

    f(i,1,N) f(j,1,M) tot += B[i][j] == '#';

    if(tot < 2)
    {
        cout << "-1";
        return 0;
    }
    if(tot == 2)
    {
        f(i,1,N) f(j,1,M) if(B[i][j] == '#' && B[i+1][j] == '#')
        {
            cout << "-1";
            return 0;
        }
        f(i,1,N) f(j,1,M) if(B[i][j] == '#' && B[i][j+1] == '#')
        {
            cout << "-1";
            return 0;
        }
    }

    int c = 0;
    f(i,1,N) f(j,1,M) if(B[i][j] == '#' && !V[i][j]) dfs(i,j), c++;
    if(c>1)
    {
        cout << "0";
        return 0;
    }

    f(i,1,N) f(j,1,M) if(B[i][j] == '#')
    {
        B[i][j] = '.';
        int comps = 0;

        f(x,1,N) f(y,1,M) V[x][y] = false;
        f(x,1,N) f(y,1,M) if(B[x][y] == '#' && !V[x][y])
        {
            comps++;
            dfs(x,y);
        }

        if(comps > 1)
        {
            cout << "1";
            return 0;
        }

        B[i][j] = '#';
    }

    cout << "2";
}


#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

struct Counter
{
	static int k;
	Counter() {k++;}
	~Counter() {k--;}
};

int Counter::k = 0;

template<typename T>
void pr(const string& name, T t)
{
    cout << name << " = " << t << endl;
}

template<typename T, typename ... Types>
void pr(const string& names, T t, Types ... rest)
{
    auto comma_pos = names.find(',');
    cout << names.substr(0, comma_pos) << " = " << t << ", ";

    auto next_name_pos = names.find_first_not_of(" \t\n", comma_pos + 1);
    pr(string(names, next_name_pos), rest ...);
}

void mod(ll &a, ll b)
{
    a %= b;
    if(a < 0) a += b;
}

#define all(x) x.begin(), x.end()
#define f(i,a,b) for(int i = (a); i <= (b); i++)
#define fd(i,a,b) for(int i = (a); i >= (b); i--)
#define mp make_pair
#define faster_io() ios_base::sync_with_stdio(false)
#define pb push_back
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
#define trace(...) pr(#__VA_ARGS__, __VA_ARGS__);
#define tracea(x) {string s = #x; Counter c##x; cout<<s.substr(0,1+s.find('['))<<Counter::k<<"]="<<x<<'\n';}
#define vii vector<pair<int,int>>

const ll MOD = 1000000007;

// ----------------------------------------------------------------------------------------------------------

bool V[55][55];
char B[55][55];
int N, M;

void dfs(int i, int j)
{
    if(B[i][j] != '#') return;
    if(V[i][j]) return;

    V[i][j] = true;

    dfs(i+1,j);
    dfs(i,j+1);
    dfs(i-1,j);
    dfs(i,j-1);
}

int main()
{
    cin >> N >> M;

    f(i,1,N)
    {
        string s;
        cin >> s;
        f(j,1,M) B[i][j] = s[j-1];
    }

    int tot = 0;

    f(i,1,N) f(j,1,M) tot += B[i][j] == '#';

    if(tot < 2)
    {
        cout << "-1";
        return 0;
    }

    int c = 0;
    f(i,1,N) f(j,1,M) if(B[i][j] == '#' && !V[i][j]) dfs(i,j), c++;
    if(c>1)
    {
        cout << "0";
        return 0;
    }

    f(i,1,N) f(j,1,M) if(B[i][j] == '#')
    {
        B[i][j] = '.';
        int comps = 0;

        f(x,1,N) f(y,1,M) V[x][y] = false;
        f(x,1,N) f(y,1,M) if(B[x][y] == '#' && !V[x][y])
        {
            comps++;
            dfs(x,y);
        }

        if(comps > 1)
        {
            cout << "1";
            return 0;
        }

        B[i][j] = '#';
    }

    cout << "2";
}


#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

struct Counter
{
	static int k;
	Counter() {k++;}
	~Counter() {k--;}
};

int Counter::k = 0;

template<typename T>
void pr(const string& name, T t)
{
    cout << name << " = " << t << endl;
}

template<typename T, typename ... Types>
void pr(const string& names, T t, Types ... rest)
{
    auto comma_pos = names.find(',');
    cout << names.substr(0, comma_pos) << " = " << t << ", ";

    auto next_name_pos = names.find_first_not_of(" \t\n", comma_pos + 1);
    pr(string(names, next_name_pos), rest ...);
}

void mod(ll &a, ll b)
{
    a %= b;
    if(a < 0) a += b;
}

#define all(x) x.begin(), x.end()
#define f(i,a,b) for(int i = (a); i <= (b); i++)
#define fd(i,a,b) for(int i = (a); i >= (b); i--)
#define mp make_pair
#define faster_io() ios_base::sync_with_stdio(false)
#define pb push_back
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
#define trace(...) pr(#__VA_ARGS__, __VA_ARGS__);
#define tracea(x) {string s = #x; Counter c##x; cout<<s.substr(0,1+s.find('['))<<Counter::k<<"]="<<x<<'\n';}
#define vii vector<pair<int,int>>

const ll MOD = 1000000007;

// ----------------------------------------------------------------------------------------------------------

char B[55][55];
int N, M;

int main()
{
    cin >> N >> M;

    f(i,1,N)
    {
        string s;
        cin >> s;
        f(j,1,M) B[i][j] = s[j-1];
    }

    int tot = 0;

    f(i,1,N) f(j,1,N) tot += B[i][j] == '#';

    if(tot == 0)
    {
        cout << "-1";
        return 0;
    }
    if(tot == 1)
    {
        cout << "1";
        return 0;
    }

    int ans = 999999;

    f(i,1,N) f(j,1,N) if(B[i][j] == '#')
    {
        int moves = 0;
        f(x,i-1,i+1) f(y,j-1,j+1) if(x==i || y==j) moves += (B[x][y] == '#');
        ans = min(ans, moves-1);
    }

    cout << ans;
}


#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

struct Counter
{
	static int k;
	Counter() {k++;}
	~Counter() {k--;}
};

int Counter::k = 0;

template<typename T>
void pr(const string& name, T t)
{
    cout << name << " = " << t << endl;
}

template<typename T, typename ... Types>
void pr(const string& names, T t, Types ... rest)
{
    auto comma_pos = names.find(',');
    cout << names.substr(0, comma_pos) << " = " << t << ", ";

    auto next_name_pos = names.find_first_not_of(" \t\n", comma_pos + 1);
    pr(string(names, next_name_pos), rest ...);
}

void mod(ll &a, ll b)
{
    a %= b;
    if(a < 0) a += b;
}

#define all(x) x.begin(), x.end()
#define f(i,a,b) for(int i = (a); i <= (b); i++)
#define fd(i,a,b) for(int i = (a); i >= (b); i--)
#define mp make_pair
#define faster_io() ios_base::sync_with_stdio(false)
#define pb push_back
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
#define trace(...) pr(#__VA_ARGS__, __VA_ARGS__);
#define tracea(x) {string s = #x; Counter c##x; cout<<s.substr(0,1+s.find('['))<<Counter::k<<"]="<<x<<'\n';}
#define vii vector<pair<int,int>>

const ll MOD = 1000000007;

// ----------------------------------------------------------------------------------------------------------

bool V[55][55];
char B[55][55];
int N, M;

void dfs(int i, int j)
{
    if(B[i][j] != '#') return;
    if(V[i][j]) return;

    V[i][j] = true;

    dfs(i+1,j);
    dfs(i,j+1);
    dfs(i-1,j);
    dfs(i,j-1);
}

int main()
{
    cin >> N >> M;

    f(i,1,N)
    {
        string s;
        cin >> s;
        f(j,1,M) B[i][j] = s[j-1];
    }

    int tot = 0;

    f(i,1,N) f(j,1,N) tot += B[i][j] == '#';

    if(tot < 2)
    {
        cout << "-1";
        return 0;
    }

    f(i,1,N) f(j,1,N) if(B[i][j] == '#')
    {
        B[i][j] = '.';
        int comps = 0;

        f(x,1,N) f(y,1,N) V[x][y] = false;
        f(x,1,N) f(y,1,N) if(B[x][y] == '#' && !V[x][y])
        {
            comps++;
            dfs(x,y);
        }

        if(comps > 1)
        {
            cout << "1";
            return 0;
        }
    }

    cout << "2";
}


#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

struct Counter
{
	static int k;
	Counter() {k++;}
	~Counter() {k--;}
};

int Counter::k = 0;

template<typename T>
void pr(const string& name, T t)
{
    cout << name << " = " << t << endl;
}

template<typename T, typename ... Types>
void pr(const string& names, T t, Types ... rest)
{
    auto comma_pos = names.find(',');
    cout << names.substr(0, comma_pos) << " = " << t << ", ";

    auto next_name_pos = names.find_first_not_of(" \t\n", comma_pos + 1);
    pr(string(names, next_name_pos), rest ...);
}

void mod(ll &a, ll b)
{
    a %= b;
    if(a < 0) a += b;
}

#define all(x) x.begin(), x.end()
#define f(i,a,b) for(int i = (a); i <= (b); i++)
#define fd(i,a,b) for(int i = (a); i >= (b); i--)
#define mp make_pair
#define faster_io() ios_base::sync_with_stdio(false)
#define pb push_back
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
#define trace(...) pr(#__VA_ARGS__, __VA_ARGS__);
#define tracea(x) {string s = #x; Counter c##x; cout<<s.substr(0,1+s.find('['))<<Counter::k<<"]="<<x<<'\n';}
#define vii vector<pair<int,int>>

const ll MOD = 1000000007;

// ----------------------------------------------------------------------------------------------------------

bool V[55][55];
char B[55][55];
int N, M;

void dfs(int i, int j)
{
    if(B[i][j] != '#') return;
    if(V[i][j]) return;

    V[i][j] = true;

    dfs(i+1,j);
    dfs(i,j+1);
    dfs(i-1,j);
    dfs(i,j-1);
}

int main()
{
    cin >> N >> M;

    f(i,1,N)
    {
        string s;
        cin >> s;
        f(j,1,M) B[i][j] = s[j-1];
    }

    int tot = 0;

    f(i,1,N) f(j,1,N) tot += B[i][j] == '#';

    if(tot < 2)
    {
        cout << "-1";
        return 0;
    }

    int c = 0;
    f(i,1,N) f(j,1,N) if(B[i][j] == '#') dfs(i,j), c++;
    if(c>1)
    {
        cout << "0";
        return 0;
    }

    f(i,1,N) f(j,1,N) if(B[i][j] == '#')
    {
        B[i][j] = '.';
        int comps = 0;

        f(x,1,N) f(y,1,N) V[x][y] = false;
        f(x,1,N) f(y,1,N) if(B[x][y] == '#' && !V[x][y])
        {
            comps++;
            dfs(x,y);
        }

        if(comps > 1)
        {
            cout << "1";
            return 0;
        }

        B[i][j] = '#';
    }

    cout << "2";
}


#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

struct Counter
{
	static int k;
	Counter() {k++;}
	~Counter() {k--;}
};

int Counter::k = 0;

template<typename T>
void pr(const string& name, T t)
{
    cout << name << " = " << t << endl;
}

template<typename T, typename ... Types>
void pr(const string& names, T t, Types ... rest)
{
    auto comma_pos = names.find(',');
    cout << names.substr(0, comma_pos) << " = " << t << ", ";

    auto next_name_pos = names.find_first_not_of(" \t\n", comma_pos + 1);
    pr(string(names, next_name_pos), rest ...);
}

void mod(ll &a, ll b)
{
    a %= b;
    if(a < 0) a += b;
}

#define all(x) x.begin(), x.end()
#define f(i,a,b) for(int i = (a); i <= (b); i++)
#define fd(i,a,b) for(int i = (a); i >= (b); i--)
#define mp make_pair
#define faster_io() ios_base::sync_with_stdio(false)
#define pb push_back
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
#define trace(...) pr(#__VA_ARGS__, __VA_ARGS__);
#define tracea(x) {string s = #x; Counter c##x; cout<<s.substr(0,1+s.find('['))<<Counter::k<<"]="<<x<<'\n';}
#define vii vector<pair<int,int>>

const ll MOD = 1000000007;

// ----------------------------------------------------------------------------------------------------------

char B[55][55];
int N, M;

int main()
{
    cin >> N >> M;

    f(i,1,N)
    {
        string s;
        cin >> s;
        f(j,1,M) B[i][j] = s[j-1];
    }

    int ans = 999999;

    f(i,1,N) f(j,1,N) if(B[i][j] == '#')
    {
        int moves = 0;
        f(x,i-1,i+1) f(y,j-1,j+1) if(x==i || y==j) moves += (B[x][y] == '#');
        ans = min(ans, moves-1);
    }

    cout << ans;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cassert>
#include <math.h>
#include <vector>
#include <time.h>
#include <set>
#include <queue>
#include <map>
#define REP(i,n) for(int i=0, _n=(n); i<_n; i++)
#define REPD(i,n) for(int i=n-1; i>=0; i--)
#define FOR(i,a,b) for(int i=a,_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=a,_b=(b); i>=_b; i--)
#define FILL(x, v) memset(&x,v,sizeof(x))
#define DB(vari) cerr<<#vari<<" = "<<vari<<endl;
#define x first
#define y second
#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;
typedef long long LL;

const int MAXN = 59;
const int MAXM = 59;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int n, m;
char field[MAXN][MAXM];
bool visited[MAXN][MAXM];

void dfs(int a, int b)
{
  visited[a][b] = 1;
  REP(k, 4)
  {
    int na = a + dx[k];
    int nb = b + dy[k];
    if (na < 0 || nb < 0)
      continue;
    if (na >= n || nb >= m)
      continue;
    if (visited[na][nb] || (field[na][nb] != '#'))
      continue;
    dfs(na, nb);
  }
}

int main()
{
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  scanf("%d%d\n", &n, &m);
  REP(i, n)
  {
    REP(j, m)
    {
      scanf("%c", &field[i][j]);
    }
    scanf("\n");
  }
  int ans = 10000000;
  int c = 0;
  REP(i, n)
  {
    REP(j, m)
    {
      if (field[i][j] != '#')
        continue;
      REP(ii, n)
      {
        REP(jj, m)
        {
          if (field[ii][jj] != '#')
            continue;
          field[i][j] = field[ii][jj] = '.';
          c = 2;
          if ((i == ii) && (j == jj))
            c = 1;
          int cnt = 0;
          REP(a, n)
            REP(b, m)
              visited[a][b] = false;
          REP(a, n)
          {
            REP(b, m)
            {
              if ((field[a][b] == '#') && !visited[a][b])
              {
                cnt++;
                dfs(a, b);
              }
              if (cnt > 1)
              {
                a = n;
                b = m;
              }
            }
          }
          if (cnt > 1)
          {
            ans = min(ans, c);
          }
          field[i][j] = field[ii][jj] = '#';
        }
      }
    }
  }
  if (ans > 100000)
    ans = -1;
  printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cassert>
#include <math.h>
#include <vector>
#include <time.h>
#include <set>
#include <queue>
#include <map>
#define REP(i,n) for(int i=0, _n=(n); i<_n; i++)
#define REPD(i,n) for(int i=n-1; i>=0; i--)
#define FOR(i,a,b) for(int i=a,_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=a,_b=(b); i>=_b; i--)
#define FILL(x, v) memset(&x,v,sizeof(x))
#define DB(vari) cerr<<#vari<<" = "<<vari<<endl;
#define x first
#define y second
#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;
typedef long long LL;

const int MAXN = 59;
const int MAXM = 59;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int n, m;
char field[MAXN][MAXM];
bool visited[MAXN][MAXM];

void dfs(int a, int b)
{
  visited[a][b] = 1;
  REP(k, 4)
  {
    int na = a + dx[k];
    int nb = b + dy[k];
    if (na < 0 || nb < 0)
      continue;
    if (visited[na][nb] || (field[na][nb] != '#'))
      continue;
    dfs(na, nb);
  }
}

int main()
{
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  scanf("%d%d\n", &n, &m);
  REP(i, n)
  {
    REP(j, m)
    {
      scanf("%c", &field[i][j]);
    }
    scanf("\n");
  }
  int ans = 10000000;
  int c = 0;
  REP(i, n)
  {
    REP(j, m)
    {
      if (field[i][j] != '#')
        continue;
      REP(ii, i + 1)
      {
        REP(jj, j + 1)
        {
          if (field[ii][jj] != '#')
            continue;
          field[i][j] = field[ii][jj] = '.';
          c = 2;
          if ((i == ii) && (j == jj))
            c = 1;
          if (ans == 2 && c != 1)
            continue;
          int cnt = 0;
          REP(a, n)
            REP(b, m)
              visited[a][b] = false;
          REP(a, n)
          {
            REP(b, m)
            {
              if ((field[a][b] == '#') && !visited[a][b])
              {
                cnt++;
                dfs(a, b);
              }
              if (cnt > 1)
              {
                a = n;
                b = m;
              }
            }
          }
          if (cnt > 1)
          {
            ans = min(ans, c);
            if (ans == 1)
            {
              i = n;
              j = m;
            }
          }
          field[i][j] = field[ii][jj] = '#';
        }
      }
    }
  }
  if (ans > 100000)
    ans = -1;
  printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cassert>
#include <math.h>
#include <vector>
#include <time.h>
#include <set>
#include <queue>
#include <map>
#define REP(i,n) for(int i=0, _n=(n); i<_n; i++)
#define REPD(i,n) for(int i=n-1; i>=0; i--)
#define FOR(i,a,b) for(int i=a,_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=a,_b=(b); i>=_b; i--)
#define FILL(x, v) memset(&x,v,sizeof(x))
#define DB(vari) cerr<<#vari<<" = "<<vari<<endl;
#define x first
#define y second
#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;
typedef long long LL;

const int MAXN = 59;
const int MAXM = 59;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int n, m;
char field[MAXN][MAXM];
bool visited[MAXN][MAXM];

void dfs(int a, int b)
{
  visited[a][b] = 1;
  REP(k, 4)
  {
    int na = a + dx[k];
    int nb = b + dy[k];
    if (na < 0 || nb < 0)
      continue;
    if (na >= n || nb >= m)
      continue;
    if (visited[na][nb] || (field[na][nb] != '#'))
      continue;
    dfs(na, nb);
  }
}

int main()
{
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  scanf("%d%d\n", &n, &m);
  REP(i, n)
  {
    REP(j, m)
    {
      scanf("%c", &field[i][j]);
    }
    scanf("\n");
  }
  int ans = 10000000;
  int c = 0;
  REP(i, n)
  {
    REP(j, m)
    {
      if (field[i][j] != '#')
        continue;
      REP(ii, n)
      {
        REP(jj, m)
        {
          if (field[ii][jj] != '#')
            continue;
          field[i][j] = field[ii][jj] = '.';
          c = 2;
          if ((i == ii) && (j == jj))
            c = 1;
          int cnt = 0;
          REP(a, n)
            REP(b, m)
              visited[a][b] = false;
          REP(a, n)
          {
            REP(b, m)
            {
              if ((field[a][b] == '#') && !visited[a][b])
              {
                cnt++;
                dfs(a, b);
              }
              if (cnt > 1)
              {
                a = n;
                b = m;
              }
            }
          }
          if (cnt > 1)
          {
            ans = min(ans, c);
            if (ans == 1)
            {
              i = n;
              j = m;
            }
          }
          field[i][j] = field[ii][jj] = '#';
        }
      }
    }
  }
  if (ans > 100000)
    ans = -1;
  printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cassert>
#include <math.h>
#include <vector>
#include <time.h>
#include <set>
#include <queue>
#include <map>
#define REP(i,n) for(int i=0, _n=(n); i<_n; i++)
#define REPD(i,n) for(int i=n-1; i>=0; i--)
#define FOR(i,a,b) for(int i=a,_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=a,_b=(b); i>=_b; i--)
#define FILL(x, v) memset(&x,v,sizeof(x))
#define DB(vari) cerr<<#vari<<" = "<<vari<<endl;
#define x first
#define y second
#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;
typedef long long LL;

const int MAXN = 59;
const int MAXM = 59;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int n, m;
char field[MAXN][MAXM];
char visited[MAXN][MAXM];
int c[MAXN][MAXM];

void dfs(int a, int b)
{
  //DB("dfs");
  //DB(a);
  //DB(b);
  visited[a][b] = 1;
  REP(k, 4)
  {
    int na = a + dx[k];
    int nb = b + dy[k];
    if (na < 0 || nb < 0)
      continue;
    if (na >= n || nb >= m)
      continue;
    if (visited[na][nb] || field[na][nb] != '#')
      continue;
    dfs(na, nb);
  }
}

int main()
{
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  scanf("%d%d\n", &n, &m);
  REP(i, n)
  {
    REP(j, m)
    {
      scanf("%c", &field[i][j]);
    }
    scanf("\n");
  }
  int ans = 10000000;
  int c = 0;
  REP(i, n)
  {
    REP(j, m)
    {
      if (field[i][j] != '#')
        continue;
      REP(ii, n)
      {
        REP(jj, m)
        {
          if (field[ii][jj] != '#')
            continue;
          field[i][j] = field[ii][jj] = '.';
          c = 2;
          if (i == ii && j == jj)
            c = 1;
          int cnt = 0;
          memset(visited, 0, sizeof(visited));
          REP(a, n)
          {
            REP(b, m)
            {
              if ((field[a][b] == '#') && !visited[a][b])
              {
                cnt++;
                dfs(a, b);
              }
            }
          }
          if (cnt > 1)
          {
            ans = min(ans, c);
            //DB(c);
            //DB(i);
            //DB(j);
          }
          field[i][j] = field[ii][jj] = '#';
        }
      }
    }
  }
  if (ans > 100000)
    ans = -1;
  printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cassert>
#include <math.h>
#include <vector>
#include <time.h>
#include <set>
#include <queue>
#include <map>
#define REP(i,n) for(int i=0, _n=(n); i<_n; i++)
#define REPD(i,n) for(int i=n-1; i>=0; i--)
#define FOR(i,a,b) for(int i=a,_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=a,_b=(b); i>=_b; i--)
#define FILL(x, v) memset(&x,v,sizeof(x))
#define DB(vari) cerr<<#vari<<" = "<<vari<<endl;
#define x first
#define y second
#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;
typedef long long LL;

const int MAXN = 51;
const int MAXM = 51;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int n, m;
char field[MAXN][MAXM];
int c[MAXN][MAXM];

int main()
{
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  scanf("%d%d\n", &n, &m);
  REP(i, n)
  {
    REP(j, m)
    {
      scanf("%c", &field[i][j]);
    }
    scanf("\n");
  }
  int ans = 10000000;
  REP(i, n)
  {
    REP(j, m)
    {
      if (field[i][j] != '#')
        continue;
      int cnt = 0;
      REP(k, 4)
      {
        int nx = i + dx[k];
        int ny = j + dy[k];
        if (nx < 0 || ny < 0 || nx >= n || ny >= m)
          continue;
        //cout << nx << " " << ny << endl;
        if (field[nx][ny] == '#')
          cnt++;
      }
      c[i][j] = cnt;
      //cout << cnt << endl;
      //ans = min(ans, cnt);
    }
  }
  REP(i, n)
  {
    REP(j, m)
    {
      if (field[i][j] != '#')
        continue;
      if (c[i][j] > 1)
        ans = min(ans, c[i][j]);
      else
      {
        REP(k, 4)
        {
          int nx = i + dx[k];
          int ny = j + dy[k];
          if (nx < 0 || ny < 0 || nx >= n || ny >= m)
            continue;
          //cout << nx << " " << ny << endl;
          if (c[nx][ny] > 1)
            ans = 1;
        }
      }
      //cout << cnt << endl;
    }
  }
  if (ans > 100000)
    ans = -1;
  printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cassert>
#include <math.h>
#include <vector>
#include <time.h>
#include <set>
#include <queue>
#include <map>
#define REP(i,n) for(int i=0, _n=(n); i<_n; i++)
#define REPD(i,n) for(int i=n-1; i>=0; i--)
#define FOR(i,a,b) for(int i=a,_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=a,_b=(b); i>=_b; i--)
#define FILL(x, v) memset(&x,v,sizeof(x))
#define DB(vari) cerr<<#vari<<" = "<<vari<<endl;
#define x first
#define y second
#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;
typedef long long LL;

const int MAXN = 59;
const int MAXM = 59;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int n, m;
char field[MAXN][MAXM];
bool visited[MAXN][MAXM];

void dfs(int a, int b)
{
  visited[a][b] = 1;
  REP(k, 4)
  {
    int na = a + dx[k];
    int nb = b + dy[k];
    if (na < 0 || nb < 0)
      continue;
    if (na >= n || nb >= m)
      continue;
    if (visited[na][nb] || (field[na][nb] != '#'))
      continue;
    dfs(na, nb);
  }
}

int main()
{
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  scanf("%d%d\n", &n, &m);
  REP(i, n)
  {
    REP(j, m)
    {
      scanf("%c", &field[i][j]);
    }
    scanf("\n");
  }
  int ans = 10000000;
  int c = 0;
  REP(i, n)
  {
    REP(j, m)
    {
      if (field[i][j] != '#')
        continue;
      REP(ii, n)
      {
        REP(jj, m)
        {
          if (field[ii][jj] != '#')
            continue;
          field[i][j] = field[ii][jj] = '.';
          c = 2;
          if ((i == ii) && (j == jj))
            c = 1;
          int cnt = 0;
          REP(a, n)
            REP(b, m)
              visited[a][b] = false;
          REP(a, n)
          {
            REP(b, m)
            {
              if ((field[a][b] == '#') && !visited[a][b])
              {
                cnt++;
                dfs(a, b);
              }
            }
          }
          if (cnt > 1)
          {
            ans = min(ans, c);
          }
          field[i][j] = field[ii][jj] = '#';
        }
      }
    }
  }
  if (ans > 100000)
    ans = -1;
  printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cassert>
#include <math.h>
#include <vector>
#include <time.h>
#include <set>
#include <queue>
#include <map>
#define REP(i,n) for(int i=0, _n=(n); i<_n; i++)
#define REPD(i,n) for(int i=n-1; i>=0; i--)
#define FOR(i,a,b) for(int i=a,_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=a,_b=(b); i>=_b; i--)
#define FILL(x, v) memset(&x,v,sizeof(x))
#define DB(vari) cerr<<#vari<<" = "<<vari<<endl;
#define x first
#define y second
#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;
typedef long long LL;

const int MAXN = 59;
const int MAXM = 59;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int n, m;
char field[MAXN][MAXM];
bool visited[MAXN][MAXM];

void dfs(int a, int b)
{
  visited[a][b] = 1;
  REP(k, 4)
  {
    int na = a + dx[k];
    int nb = b + dy[k];
    if (na < 0 || nb < 0)
      continue;
    if (visited[na][nb] || (field[na][nb] != '#'))
      continue;
    dfs(na, nb);
  }
}

int main()
{
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  scanf("%d%d\n", &n, &m);
  REP(i, n)
  {
    REP(j, m)
    {
      scanf("%c", &field[i][j]);
    }
    scanf("\n");
  }
  int ans = 10000000;
  int c = 0;
  REP(i, n)
  {
    REP(j, m)
    {
      if (field[i][j] != '#')
        continue;
      REP(ii, i + 1)
      {
        REP(jj, j + 1)
        {
          if (field[ii][jj] != '#')
            continue;
          field[i][j] = field[ii][jj] = '.';
          c = 2;
          if ((i == ii) && (j == jj))
            c = 1;
          int cnt = 0;
          REP(a, n)
            REP(b, m)
              visited[a][b] = false;
          REP(a, n)
          {
            REP(b, m)
            {
              if ((field[a][b] == '#') && !visited[a][b])
              {
                cnt++;
                dfs(a, b);
              }
              if (cnt > 1)
              {
                a = n;
                b = m;
              }
            }
          }
          if (cnt > 1)
          {
            ans = min(ans, c);
            if (ans == 1)
            {
              i = n;
              j = m;
            }
          }
          field[i][j] = field[ii][jj] = '#';
        }
      }
    }
  }
  if (ans > 100000)
    ans = -1;
  printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cassert>
#include <math.h>
#include <vector>
#include <time.h>
#include <set>
#include <queue>
#include <map>
#define REP(i,n) for(int i=0, _n=(n); i<_n; i++)
#define REPD(i,n) for(int i=n-1; i>=0; i--)
#define FOR(i,a,b) for(int i=a,_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=a,_b=(b); i>=_b; i--)
#define FILL(x, v) memset(&x,v,sizeof(x))
#define DB(vari) cerr<<#vari<<" = "<<vari<<endl;
#define x first
#define y second
#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;
typedef long long LL;

const int MAXN = 51;
const int MAXM = 51;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int n, m;
char field[MAXN][MAXM];

int main()
{
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  scanf("%d%d\n", &n, &m);
  REP(i, n)
  {
    REP(j, m)
    {
      scanf("%c", &field[i][j]);
    }
    scanf("\n");
  }
  int ans = 10000000;
  REP(i, n)
  {
    REP(j, m)
    {
      if (field[i][j] != '#')
        continue;
      int cnt = 0;
      REP(k, 4)
      {
        int nx = i + dx[k];
        int ny = j + dy[k];
        if (nx < 0 || ny < 0 || nx >= n || ny >= m)
          continue;
        //cout << nx << " " << ny << endl;
        if (field[nx][ny] == '#')
          cnt++;
      }
      //cout << cnt << endl;
      ans = min(ans, cnt);
    }
  }
  if (ans > 100000)
    ans = -1;
  printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cassert>
#include <math.h>
#include <vector>
#include <time.h>
#include <set>
#include <queue>
#include <map>
#define REP(i,n) for(int i=0, _n=(n); i<_n; i++)
#define REPD(i,n) for(int i=n-1; i>=0; i--)
#define FOR(i,a,b) for(int i=a,_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=a,_b=(b); i>=_b; i--)
#define FILL(x, v) memset(&x,v,sizeof(x))
#define DB(vari) cerr<<#vari<<" = "<<vari<<endl;
#define x first
#define y second
#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;
typedef long long LL;

const int MAXN = 59;
const int MAXM = 59;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int n, m;
char field[MAXN][MAXM];
bool visited[MAXN][MAXM];

void dfs(int a, int b)
{
  visited[a][b] = 1;
  REP(k, 4)
  {
    int na = a + dx[k];
    int nb = b + dy[k];
    if (na < 0 || nb < 0)
      continue;
    if (visited[na][nb] || (field[na][nb] != '#'))
      continue;
    dfs(na, nb);
  }
}

int main()
{
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  scanf("%d%d\n", &n, &m);
  REP(i, n)
  {
    REP(j, m)
    {
      scanf("%c", &field[i][j]);
    }
    scanf("\n");
  }
  int ans = 10000000;
  int c = 0;
  REP(i, n)
  {
    REP(j, m)
    {
      //DB(field[i][j]);
      if (field[i][j] != '#')
        continue;
      REP(ii, n)
      {
        REP(jj, m)
        {
          if (field[ii][jj] != '#')
            continue;
          field[i][j] = field[ii][jj] = '.';
          c = 2;
          if ((i == ii) && (j == jj))
            c = 1;
          if (ans != 2 || c == 1)
          {
            int cnt = 0;
            REP(a, n)
              REP(b, m)
                visited[a][b] = false;
            REP(a, n)
            {
              REP(b, m)
              {
                if ((field[a][b] == '#') && !visited[a][b])
                {
                  cnt++;
                  dfs(a, b);
                }
                if (cnt > 1)
                {
                  a = n;
                  b = m;
                }
              }
            }
            if (cnt > 1)
            {
              ans = min(ans, c);
              if (ans == 1)
              {
                i = n;
                j = m;
              }
            }
          }
            field[i][j] = field[ii][jj] = '#';
        }
      }
    }
  }
  if (ans > 100000)
    ans = -1;
  printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cassert>
#include <math.h>
#include <vector>
#include <time.h>
#include <set>
#include <queue>
#include <map>
#define REP(i,n) for(int i=0, _n=(n); i<_n; i++)
#define REPD(i,n) for(int i=n-1; i>=0; i--)
#define FOR(i,a,b) for(int i=a,_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=a,_b=(b); i>=_b; i--)
#define FILL(x, v) memset(&x,v,sizeof(x))
#define DB(vari) cerr<<#vari<<" = "<<vari<<endl;
#define x first
#define y second
#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;
typedef long long LL;

const int MAXN = 59;
const int MAXM = 59;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int n, m;
char field[MAXN][MAXM];
bool visited[MAXN][MAXM];

void dfs(int a, int b)
{
  visited[a][b] = 1;
  REP(k, 4)
  {
    int na = a + dx[k];
    int nb = b + dy[k];
    if (na < 0 || nb < 0)
      continue;
    if (visited[na][nb] || (field[na][nb] != '#'))
      continue;
    dfs(na, nb);
  }
}

int main()
{
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  scanf("%d%d\n", &n, &m);
  REP(i, n)
  {
    REP(j, m)
    {
      scanf("%c", &field[i][j]);
    }
    scanf("\n");
  }
  int ans = 10000000;
  int c = 0;
  REP(i, n)
  {
    REP(j, m)
    {
      if (field[i][j] != '#')
        continue;
      REP(ii, n)
      {
        REP(jj, m)
        {
          if (field[ii][jj] != '#')
            continue;
          field[i][j] = field[ii][jj] = '.';
          c = 2;
          if ((i == ii) && (j == jj))
            c = 1;
          if (ans == 2 && c != 1)
            continue;
          int cnt = 0;
          REP(a, n)
            REP(b, m)
              visited[a][b] = false;
          REP(a, n)
          {
            REP(b, m)
            {
              if ((field[a][b] == '#') && !visited[a][b])
              {
                cnt++;
                dfs(a, b);
              }
              if (cnt > 1)
              {
                a = n;
                b = m;
              }
            }
          }
          if (cnt > 1)
          {
            ans = min(ans, c);
            if (ans == 1)
            {
              i = n;
              j = m;
            }
          }
          field[i][j] = field[ii][jj] = '#';
        }
      }
    }
  }
  if (ans > 100000)
    ans = -1;
  printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cassert>
#include <math.h>
#include <vector>
#include <time.h>
#include <set>
#include <queue>
#include <map>
#define REP(i,n) for(int i=0, _n=(n); i<_n; i++)
#define REPD(i,n) for(int i=n-1; i>=0; i--)
#define FOR(i,a,b) for(int i=a,_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=a,_b=(b); i>=_b; i--)
#define FILL(x, v) memset(&x,v,sizeof(x))
#define DB(vari) cerr<<#vari<<" = "<<vari<<endl;
#define x first
#define y second
#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;
typedef long long LL;

const int MAXN = 59;
const int MAXM = 59;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int n, m;
char field[MAXN][MAXM];
bool visited[MAXN][MAXM];

void dfs(int a, int b)
{
  visited[a][b] = 1;
  REP(k, 4)
  {
    int na = a + dx[k];
    int nb = b + dy[k];
    if (na < 0 || nb < 0)
      continue;
    if (na >= n || nb >= m)
      continue;
    if (!visited[na][nb] || (field[na][nb] != '#'))
      continue;
    dfs(na, nb);
  }
}

int main()
{
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  scanf("%d%d\n", &n, &m);
  REP(i, n)
  {
    REP(j, m)
    {
      scanf("%c", &field[i][j]);
    }
    scanf("\n");
  }
  int ans = 10000000;
  int c = 0;
  REP(i, n)
  {
    REP(j, m)
    {
      if (field[i][j] != '#')
        continue;
      REP(ii, n)
      {
        REP(jj, m)
        {
          if (field[ii][jj] != '#')
            continue;
          field[i][j] = field[ii][jj] = '.';
          c = 2;
          if ((i == ii) && (j == jj))
            c = 1;
          int cnt = 0;
          REP(a, n)
            REP(b, m)
              visited[a][b] = false;
          REP(a, n)
          {
            REP(b, m)
            {
              if ((field[a][b] == '#') && !visited[a][b])
              {
                cnt++;
                dfs(a, b);
              }
              if (cnt > 1)
              {
                a = n;
                b = m;
              }
            }
          }
          if (cnt > 1)
          {
            ans = min(ans, c);
            if (ans == 1)
            {
              i = n;
              j = m;
            }
          }
          field[i][j] = field[ii][jj] = '#';
        }
      }
    }
  }
  if (ans > 100000)
    ans = -1;
  printf("%d\n", ans);
	return 0;
}


#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
#include <iomanip>
#include <cstring>

#define int ll
#define all(v)  (v).begin(), (v).end()
#define rall(v)  (v).rbegin(), (v).rend()
#define pb push_back
#define eb emplace_back
#define SZ(a) ((int)((a).size()))
#define rep(i,n) for(int i = 0; i < (n); i++)

#define fst get<0>
#define snd get<1>
#define mp make_tuple

#ifndef ONLINE_JUDGE
#define _tr(x) #x<<" = "<<(x)
#define trace(x) {cerr<<_tr(x)<<endl;}
#define trace2(x,y) {cerr<<_tr(x)<<" "<<_tr(y)<<endl;}
#define trace3(x,y,z) {cerr<<_tr(x)<<" "<<_tr(y)<<" "<<_tr(z)<<endl;}
#else
#define trace(x) {}
#define trace2(x,y) {}
#define trace3(x,y,z) {}
#endif

#define VECTOR vector<T>
#define TUPLE tuple<A,B>
#define DEF_READER(value_type, ...) template<__VA_ARGS__> istream& operator>>(istream& is, value_type &x)
#define DEF_WRITER(value_type, ...) template<__VA_ARGS__> ostream& operator<<(ostream& os, value_type &x)

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef tuple<int,int> int2;

DEF_READER(VECTOR, class T){ rep(i,(int)x.size()) is >> x[i]; return is; }
DEF_READER(TUPLE, class A, class B){ is >> fst(x) >> snd(x); return is; }
DEF_WRITER(VECTOR, class T){ os << "["; rep(i,(int)x.size()) if(i != x.size()-1) os << x[i] << " "; os << "]"; return os; }
DEF_WRITER(TUPLE, class A, class B){ os << "(" << fst(x) << "," << snd(x) << ")"; return os; }

inline int divup(int x,int y) { return x/y+0<x%y; }
inline bool range(int x, int a, int b) { return a <= x && x <= b; }

const int INF = 1LL << 60;

int n,m;

bool visited[55][55];

int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };

int dfs(const vs& f, int y, int x){
  visited[y][x] = true;
  int ans = 1;
  rep(i,4){
    int newy = y + dy[i];
    int newx = x + dx[i];
    if(range(newy, 0, n-1) && range(newx, 0, m-1) && f[newy][newx] == '#' && !visited[newy][newx])
      ans += dfs(f, newy, newx);
  }
  return ans;
}

int connected(const vs& f){
  int starty = -1, startx = -1;
  int cnt = 0;
  rep(i,n){
    rep(j,m){
      if(f[i][j] == '#') {
        starty = i;
        startx = j;
        cnt++;
      }
    }
  }
  if(cnt <= 1) return true;
  memset(visited, false, sizeof(visited));
  int tmp = dfs(f, starty, startx);
  return tmp == cnt;
}

void run() {

  /* entry point */
  cin >> n >> m;

  vs f(n);

  cin >> f;

  int cnt = 0;
  rep(i, n) rep(j,m) if(f[i][j] == '#') cnt++;

  if(cnt <= 2) {
    cout << -1 << endl;
    return;
  }

  rep(i, n){
    rep(j,m){
      if(f[i][j] == '.') continue;
      vs newf = f;
      newf[i][j] = '.';
      if(!connected(newf)) {
        cout << 1 << endl;
        return;
      }
    }
  }

  cout << 2 << endl;

}

#undef int
int main() { run(); return 0; }


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

#define ALL(x) (x).begin(), (x).end()
#define MP make_pair
#define SZ(x) ((int) (x).size())
#define maxit(x,y) ((x) = max((x),(y)))
#define minit(x,y) ((x) = min((x),(y)))
typedef long long LL;

int R, C;
char field[52][52];

int N;
int nodes[2700];
bool vis[2700];
int deleted;
int Q[2700], Qb, Qe;

bool isnode(int u)
{
	if (u < 0 || u == deleted)
		return false;
	int r = u / 51;
	int c = u % 51;
	if (r < 0 || r >= R || c < 0 || c >= C)
		return false;
	if (field[r][c] == '#')
		return true;
	return false;
}

void addnode(int u)
{
	if (!isnode(u) || vis[u])
		return;
	vis[u] = true;
	Q[Qe++] = u;
}

int count_nodes(int start)
{
	int cnt = 0;
	memset(vis, 0, sizeof vis);
	Qb = 0, Qe = 1;
	Q[0] = start;
	vis[start] = true;
	while (Qb < Qe) {
		cnt += 1;
		int u = Q[Qb++];
		addnode(u-51);
		addnode(u+51);
		addnode(u-1);
		addnode(u+1);
	}
	return cnt;
}

int main()
{
	scanf("%d %d", &R, &C);
	for (int r = 0; r < R; ++r) {
		scanf("%s", field[r]);
		for (int c = 0; c < C; ++c) {
			if (field[r][c] == '.')
				continue;
			nodes[N++] = 51*r + c;
		}
	}

	if (N <= 2) {
		puts("-1");
		return 0;
	}

	int s = nodes[0];
	int mn = 0;
	if (isnode(s+1))
		mn += 1;
	if (isnode(s+51))
		mn += 1;

	for (int i = 0; i < N; ++i) {
		deleted = nodes[i];
		int cnt = 0;
		if (isnode(deleted-1))
			cnt = count_nodes(deleted-1);
		else if (isnode(deleted+1))
			cnt = count_nodes(deleted+1);
		else if (isnode(deleted-51))
			cnt = count_nodes(deleted-51);
		else if (isnode(deleted+51))
			cnt = count_nodes(deleted+51);
		if (cnt != N-1) {
			mn = 1;
			break;
		}
	}

	printf("%d\n", mn);
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
int Min=2,sum=0,SUM=0;
int COL,ROW,x=0,y=0;
int min(int a, int b)
{ return (a < b)? a: b; }

int MIN(int a, int b, int c)
{ return min(min(a, b), c);}

int max(int a, int b)
{ return (a > b)? a: b; }

void diagonalOrder(int matrix[][COL])
{
    // There will be ROW+COL-1 lines in the output
    for (int line=1; line<=(ROW + COL -1); line++)
    {
        int p=0,r,c;
        /* Get column index of the first element in this line of output.
           The index is 0 for first ROW lines and line - ROW for remaining
           lines  */
        int start_col =  max(0, line-ROW);

        /* Get count of elements in this line. The count of elements is
           equal to minimum of line number, COL-start_col and ROW */
         int count = MIN(line, (COL-start_col), ROW);

        /* Print elements of this line */
        for (int j=0; j<count; j++)
        {
            sum=sum+matrix[min(ROW, line)-j-1][start_col+j];
            if(matrix[min(ROW, line)-j-1][start_col+j]!=0&&!(line==1&&j==0)&&!(line==ROW+COL-1&&j==count-1))
                {
                    p++;
                    r=min(ROW, line)-j-1;
                    c=start_col+j;
                }
        }

        if(p==1&&sum>matrix[r][c]&&sum<SUM)
        {
            Min=1;
        }
    }
}

void CheckSpiral(int matrix[][COL])
{
    int count=0,s=0,i;
    for(i=x;i<ROW-x;i++)
    {
        s=s+matrix[i][y];
        if(matrix[i][y]!=0)
        {
            count++;
        }
    }
    for(i=x;i<ROW-x;i++)
    {
        s=s+matrix[i][ROW-y-1];
        if(matrix[i][ROW-y-1]!=0)
        {
            count++;
        }
    }
    for(i=y;i<COL-y;i++)
    {
        s=s+matrix[x][i];
        if(matrix[x][i]!=0)
        {
            count++;
        }
    }
    for(i=y;i<COL-y;i++)
    {
        s=s+matrix[COL-x-1][i];
        if(matrix[COL-x-1][i]!=0)
        {
            count++;
        }
    }
    x++;
    y++;
    if(s==2&&count==1)
    {
        Min=1;
    }
    else if(x<ROW/2&&y<COL/2)
    {
        CheckSpiral(matrix);
    }
}
// Driver program to test above functions
int main()
{
    int m,n,i,j;
    scanf("%d%d",&ROW,&COL);
    n=ROW;
    m=COL;
    int a[n][m],b[n][m];
    char s[n][m];
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(j==0)
            {
                scanf(" %c",&s[i][j]);
            }
            else
            {
                scanf("%c",&s[i][j]);
            }

        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            a[i][j]=0;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                if(s[i+1][j]=='#'&&i!=n-1)
                {
                    a[i][j]++;
                }
                if(s[i-1][j]=='#'&&i!=0)
                {
                    a[i][j]++;
                }
                if(s[i][j+1]=='#'&&j!=m-1)
                {
                    a[i][j]++;
                }
                if(s[i][j-1]=='#'&&j!=0)
                {
                    a[i][j]++;
                }
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]<Min&&a[i][j]!=0)
            {
                Min=a[i][j];
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            SUM=SUM+a[i][j];
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            b[i][m-j-1]=a[i][j];
        }
    }
    if(Min!=1)
    {
        diagonalOrder(a);
    }
    if(Min!=1)
    {
        diagonalOrder(b);
    }

    int count=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                count++;
            }
        }
    }
    if(count==1||count==2)
    {
        Min=-1;
    }
    printf("%d",Min);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
int Min=2,sum=0,SUM=0;
int COL,ROW,x=0,y=0;
int min(int a, int b)
{ return (a < b)? a: b; }

int MIN(int a, int b, int c)
{ return min(min(a, b), c);}

int max(int a, int b)
{ return (a > b)? a: b; }

void diagonalOrder(int matrix[][COL])
{
    // There will be ROW+COL-1 lines in the output
    for (int line=1; line<=(ROW + COL -1); line++)
    {
        int p=0,r,c;
        /* Get column index of the first element in this line of output.
           The index is 0 for first ROW lines and line - ROW for remaining
           lines  */
        int start_col =  max(0, line-ROW);

        /* Get count of elements in this line. The count of elements is
           equal to minimum of line number, COL-start_col and ROW */
         int count = MIN(line, (COL-start_col), ROW);

        /* Print elements of this line */
        for (int j=0; j<count; j++)
        {
            sum=sum+matrix[min(ROW, line)-j-1][start_col+j];
            if(matrix[min(ROW, line)-j-1][start_col+j]!=0&&!(line==1&&j==0)&&!(line==ROW+COL-1&&j==count-1))
                {
                    p++;
                    r=min(ROW, line)-j-1;
                    c=start_col+j;
                }
        }

        if(p==1&&sum>matrix[r][c]&&sum<SUM)
        {
            Min=1;
        }
    }
}

void CheckSpiral(int matrix[][COL])
{
    int count=0,s=0,i;
    for(i=x;i<ROW-x;i++)
    {
        s=s+matrix[i][y];
        if(matrix[i][y]!=0)
        {
            count++;
        }
    }
    for(i=x;i<ROW-x;i++)
    {
        s=s+matrix[i][ROW-y-1];
        if(matrix[i][ROW-y-1]!=0)
        {
            count++;
        }
    }
    for(i=y;i<COL-y;i++)
    {
        s=s+matrix[x][i];
        if(matrix[x][i]!=0)
        {
            count++;
        }
    }
    for(i=y;i<COL-y;i++)
    {
        s=s+matrix[COL-x-1][i];
        if(matrix[COL-x-1][i]!=0)
        {
            count++;
        }
    }
    x++;
    y++;
    if(s==2&&count==1)
    {
        Min=1;
    }
    else if(x<=ROW/2&&y<=COL/2)
    {
        CheckSpiral(matrix);
    }
}
// Driver program to test above functions
int main()
{
    int m,n,i,j;
    scanf("%d%d",&ROW,&COL);
    n=ROW;
    m=COL;
    int a[n][m],b[n][m];
    char s[n][m];
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(j==0)
            {
                scanf(" %c",&s[i][j]);
            }
            else
            {
                scanf("%c",&s[i][j]);
            }

        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            a[i][j]=0;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                if(s[i+1][j]=='#'&&i!=n-1)
                {
                    a[i][j]++;
                }
                if(s[i-1][j]=='#'&&i!=0)
                {
                    a[i][j]++;
                }
                if(s[i][j+1]=='#'&&j!=m-1)
                {
                    a[i][j]++;
                }
                if(s[i][j-1]=='#'&&j!=0)
                {
                    a[i][j]++;
                }
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]<Min&&a[i][j]!=0)
            {
                Min=a[i][j];
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            SUM=SUM+a[i][j];
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            b[i][m-j-1]=a[i][j];
        }
    }
    if(Min!=1)
    {
        diagonalOrder(a);
    }
    if(Min!=1)
    {
        diagonalOrder(b);
    }

    int count=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                count++;
            }
        }
    }
    if(count==1||count==2)
    {
        Min=-1;
    }
    printf("%d",Min);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
int Min=2,sum=0,SUM=0;
int COL,ROW;
int min(int a, int b)
{ return (a < b)? a: b; }

int MIN(int a, int b, int c)
{ return min(min(a, b), c);}

int max(int a, int b)
{ return (a > b)? a: b; }

void diagonalOrder(int matrix[][COL])
{
    // There will be ROW+COL-1 lines in the output
    for (int line=1; line<=(ROW + COL -1); line++)
    {
        int p=0,r,c;
        /* Get column index of the first element in this line of output.
           The index is 0 for first ROW lines and line - ROW for remaining
           lines  */
        int start_col =  max(0, line-ROW);

        /* Get count of elements in this line. The count of elements is
           equal to minimum of line number, COL-start_col and ROW */
         int count = MIN(line, (COL-start_col), ROW);

        /* Print elements of this line */
        for (int j=0; j<count; j++)
        {
            sum=sum+matrix[min(ROW, line)-j-1][start_col+j];
            if(matrix[min(ROW, line)-j-1][start_col+j]!=0&&!(line==1&&j==0)&&!(line==ROW+COL-1&&j==count-1))
                {
                    p++;
                    r=min(ROW, line)-j-1;
                    c=start_col+j;
                }
        }

        if(p==1&&sum>matrix[r][c]&&sum<SUM)
        {
            Min=1;
        }
    }
}

// Driver program to test above functions
int main()
{
    int m,n,i,j;
    scanf("%d%d",&ROW,&COL);
    n=ROW;
    m=COL;
    int a[n][m],b[n][m];
    char s[n][m];
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(j==0)
            {
                scanf(" %c",&s[i][j]);
            }
            else
            {
                scanf("%c",&s[i][j]);
            }

        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            a[i][j]=0;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                if(s[i+1][j]=='#'&&i!=n-1)
                {
                    a[i][j]++;
                }
                if(s[i-1][j]=='#'&&i!=0)
                {
                    a[i][j]++;
                }
                if(s[i][j+1]=='#'&&j!=m-1)
                {
                    a[i][j]++;
                }
                if(s[i][j-1]=='#'&&j!=0)
                {
                    a[i][j]++;
                }
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]<Min&&a[i][j]!=0)
            {
                Min=a[i][j];
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            SUM=SUM+a[i][j];
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            b[i][m-j-1]=a[i][j];
        }
    }
    if(Min!=1)
    {
        diagonalOrder(a);
    }
    if(Min!=1)
    {
        diagonalOrder(b);
    }
    int count=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                count++;
            }
        }
    }
    if(count==1)
    {
        Min=-1;
    }
    printf("%d",Min);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
int Min=2,sum=0,SUM=0;
int COL,ROW,x=0,y=0;
int min(int a, int b)
{ return (a < b)? a: b; }

int MIN(int a, int b, int c)
{ return min(min(a, b), c);}

int max(int a, int b)
{ return (a > b)? a: b; }

void diagonalOrder(int matrix[][COL])
{
    // There will be ROW+COL-1 lines in the output
    for (int line=1; line<=(ROW + COL -1); line++)
    {
        int p=0,r,c;
        /* Get column index of the first element in this line of output.
           The index is 0 for first ROW lines and line - ROW for remaining
           lines  */
        int start_col =  max(0, line-ROW);

        /* Get count of elements in this line. The count of elements is
           equal to minimum of line number, COL-start_col and ROW */
         int count = MIN(line, (COL-start_col), ROW);

        /* Print elements of this line */
        for (int j=0; j<count; j++)
        {
            sum=sum+matrix[min(ROW, line)-j-1][start_col+j];
            if(matrix[min(ROW, line)-j-1][start_col+j]!=0&&!(line==1&&j==0)&&!(line==ROW+COL-1&&j==count-1))
                {
                    p++;
                    r=min(ROW, line)-j-1;
                    c=start_col+j;
                }
        }

        if(p==1&&sum>matrix[r][c]&&sum<SUM)
        {
            Min=1;
        }
    }
}
void CheckSpiral(int matrix[][COL])
{
    int count=0,s=0,i;
    for(i=x;i<ROW-x;i++)
    {
        s=s+matrix[i][y];
        if(matrix[i][y]!=0)
        {
            count++;
        }
    }
    for(i=x;i<ROW-x;i++)
    {
        s=s+matrix[i][ROW-y-1];
        if(matrix[i][ROW-y-1]!=0)
        {
            count++;
        }
    }
    for(i=y;i<COL-y;i++)
    {
        s=s+matrix[x][i];
        if(matrix[x][i]!=0)
        {
            count++;
        }
    }
    for(i=y;i<COL-y;i++)
    {
        s=s+matrix[COL-x-1][i];
        if(matrix[COL-x-1][i]!=0)
        {
            count++;
        }
    }
    x++;
    y++;
    if(s==2&&count==1)
    {
        Min=1;
    }
    else if(x<ROW/2&&y<COL/2)
    {
        CheckSpiral(matrix);
    }
}
// Driver program to test above functions
int main()
{
    int m,n,i,j;
    scanf("%d%d",&ROW,&COL);
    n=ROW;
    m=COL;
    int a[n][m],b[n][m];
    char s[n][m];
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(j==0)
            {
                scanf(" %c",&s[i][j]);
            }
            else
            {
                scanf("%c",&s[i][j]);
            }

        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            a[i][j]=0;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                if(s[i+1][j]=='#'&&i!=n-1)
                {
                    a[i][j]++;
                }
                if(s[i-1][j]=='#'&&i!=0)
                {
                    a[i][j]++;
                }
                if(s[i][j+1]=='#'&&j!=m-1)
                {
                    a[i][j]++;
                }
                if(s[i][j-1]=='#'&&j!=0)
                {
                    a[i][j]++;
                }
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]<Min&&a[i][j]!=0)
            {
                Min=a[i][j];
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            SUM=SUM+a[i][j];
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            b[i][m-j-1]=a[i][j];
        }
    }
    if(Min!=1)
    {
        diagonalOrder(a);
    }
    if(Min!=1)
    {
        diagonalOrder(b);
    }
    int count=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                count++;
            }
        }
    }
    if(Min!=1)
    CheckSpiral(a);
    if(count==1&&count==2)
    {
        Min=-1;
    }
    printf("%d",Min);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
int Min=2,sum=0,SUM=0;
int COL,ROW,x=0,y=0;
int min(int a, int b)
{ return (a < b)? a: b; }

int MIN(int a, int b, int c)
{ return min(min(a, b), c);}

int max(int a, int b)
{ return (a > b)? a: b; }

void diagonalOrder(int matrix[][COL])
{
    // There will be ROW+COL-1 lines in the output
    for (int line=1; line<=(ROW + COL -1); line++)
    {
        int p=0,r,c;
        /* Get column index of the first element in this line of output.
           The index is 0 for first ROW lines and line - ROW for remaining
           lines  */
        int start_col =  max(0, line-ROW);

        /* Get count of elements in this line. The count of elements is
           equal to minimum of line number, COL-start_col and ROW */
         int count = MIN(line, (COL-start_col), ROW);

        /* Print elements of this line */
        for (int j=0; j<count; j++)
        {
            sum=sum+matrix[min(ROW, line)-j-1][start_col+j];
            if(matrix[min(ROW, line)-j-1][start_col+j]!=0&&!(line==1&&j==0)&&!(line==ROW+COL-1&&j==count-1))
                {
                    p++;
                    r=min(ROW, line)-j-1;
                    c=start_col+j;
                }
        }

        if(p==1&&sum>matrix[r][c]&&sum<SUM)
        {
            Min=1;
        }
    }
}

void CheckSpiral(int matrix[][COL])
{
    int count=0,s=0,i;
    for(i=x;i<ROW-x;i++)
    {
        s=s+matrix[i][x];
        if(matrix[i][x]!=0)
        {
            count++;
        }
    }
    for(i=x;i<ROW-x;i++)
    {
        s=s+matrix[i][COL-x-1];
        if(matrix[i][COL-x-1]!=0)
        {
            count++;
        }
    }
    for(i=x;i<COL-x;i++)
    {
        s=s+matrix[x][i];
        if(matrix[x][i]!=0)
        {
            count++;
        }
    }
    for(i=x;i<COL-x;i++)
    {
        s=s+matrix[ROW-x-1][i];
        if(matrix[ROW-x-1][i]!=0)
        {
            count++;
        }
    }
    x++;
    if(s==2&&count==1)
    {
        Min=1;
    }
    else if(x<ROW/2&&x<COL/2)
    {
        CheckSpiral(matrix);
    }
}
// Driver program to test above functions
int main()
{
    int m,n,i,j;
    scanf("%d%d",&ROW,&COL);
    n=ROW;
    m=COL;
    int a[n][m],b[n][m];
    char s[n][m];
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(j==0)
            {
                scanf(" %c",&s[i][j]);
            }
            else
            {
                scanf("%c",&s[i][j]);
            }

        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            a[i][j]=0;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                if(s[i+1][j]=='#'&&i!=n-1)
                {
                    a[i][j]++;
                }
                if(s[i-1][j]=='#'&&i!=0)
                {
                    a[i][j]++;
                }
                if(s[i][j+1]=='#'&&j!=m-1)
                {
                    a[i][j]++;
                }
                if(s[i][j-1]=='#'&&j!=0)
                {
                    a[i][j]++;
                }
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]<Min&&a[i][j]!=0)
            {
                Min=a[i][j];
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            SUM=SUM+a[i][j];
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            b[i][m-j-1]=a[i][j];
        }
    }
    if(Min!=1)
    {
        diagonalOrder(a);
    }
    if(Min!=1)
    {
        diagonalOrder(b);
    }
    if(Min!=1)
    CheckSpiral(a);
    int count=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                count++;
            }
        }
    }
    if(count==1||count==2)
    {
        Min=-1;
    }
    printf("%d",Min);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
int Min=2,sum=0,SUM=0;
int COL,ROW,x=0,y=0;
int min(int a, int b)
{ return (a < b)? a: b; }

int MIN(int a, int b, int c)
{ return min(min(a, b), c);}

int max(int a, int b)
{ return (a > b)? a: b; }

void diagonalOrder(int matrix[][COL])
{
    // There will be ROW+COL-1 lines in the output
    for (int line=1; line<=(ROW + COL -1); line++)
    {
        int p=0,r,c;
        /* Get column index of the first element in this line of output.
           The index is 0 for first ROW lines and line - ROW for remaining
           lines  */
        int start_col =  max(0, line-ROW);

        /* Get count of elements in this line. The count of elements is
           equal to minimum of line number, COL-start_col and ROW */
         int count = MIN(line, (COL-start_col), ROW);

        /* Print elements of this line */
        for (int j=0; j<count; j++)
        {
            sum=sum+matrix[min(ROW, line)-j-1][start_col+j];
            if(matrix[min(ROW, line)-j-1][start_col+j]!=0&&!(line==1&&j==0)&&!(line==ROW+COL-1&&j==count-1))
                {
                    p++;
                    r=min(ROW, line)-j-1;
                    c=start_col+j;
                }
        }

        if(p==1&&sum>matrix[r][c]&&sum<SUM)
        {
            Min=1;
        }
    }
}

void CheckSpiral(int matrix[][COL])
{
    int count=0,s=0,i;
    for(i=x;i<ROW-x;i++)
    {
        s=s+matrix[i][y];
        if(matrix[i][y]!=0)
        {
            count++;
        }
    }
    for(i=x;i<ROW-x;i++)
    {
        s=s+matrix[i][ROW-y-1];
        if(matrix[i][ROW-y-1]!=0)
        {
            count++;
        }
    }
    for(i=y;i<COL-y;i++)
    {
        s=s+matrix[x][i];
        if(matrix[x][i]!=0)
        {
            count++;
        }
    }
    for(i=y;i<COL-y;i++)
    {
        s=s+matrix[COL-x-1][i];
        if(matrix[COL-x-1][i]!=0)
        {
            count++;
        }
    }
    x++;
    y++;
    if(s==2&&count==1)
    {
        Min=1;
    }
    else if(x<ROW/2&&y<COL/2)
    {
        CheckSpiral(matrix);
    }
}
// Driver program to test above functions
int main()
{
    int m,n,i,j;
    scanf("%d%d",&ROW,&COL);
    n=ROW;
    m=COL;
    int a[n][m],b[n][m];
    char s[n][m];
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(j==0)
            {
                scanf(" %c",&s[i][j]);
            }
            else
            {
                scanf("%c",&s[i][j]);
            }

        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            a[i][j]=0;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                if(s[i+1][j]=='#'&&i!=n-1)
                {
                    a[i][j]++;
                }
                if(s[i-1][j]=='#'&&i!=0)
                {
                    a[i][j]++;
                }
                if(s[i][j+1]=='#'&&j!=m-1)
                {
                    a[i][j]++;
                }
                if(s[i][j-1]=='#'&&j!=0)
                {
                    a[i][j]++;
                }
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]<Min&&a[i][j]!=0)
            {
                Min=a[i][j];
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            SUM=SUM+a[i][j];
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            b[i][m-j-1]=a[i][j];
        }
    }
    if(Min!=1)
    {
        diagonalOrder(a);
    }
    if(Min!=1)
    {
        diagonalOrder(b);
    }
    if(Min!=1)
    CheckSpiral(a);
    int count=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                count++;
            }
        }
    }
    if(count==1||count==2)
    {
        Min=-1;
    }
    printf("%d",Min);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
int Min=2,sum=0,SUM=0;
int COL,ROW,x=0,y=0;
int min(int a, int b)
{ return (a < b)? a: b; }

int MIN(int a, int b, int c)
{ return min(min(a, b), c);}

int max(int a, int b)
{ return (a > b)? a: b; }

void diagonalOrder(int matrix[][COL])
{
    // There will be ROW+COL-1 lines in the output
    for (int line=1; line<=(ROW + COL -1); line++)
    {
        int p=0,r,c;
        /* Get column index of the first element in this line of output.
           The index is 0 for first ROW lines and line - ROW for remaining
           lines  */
        int start_col =  max(0, line-ROW);

        /* Get count of elements in this line. The count of elements is
           equal to minimum of line number, COL-start_col and ROW */
         int count = MIN(line, (COL-start_col), ROW);

        /* Print elements of this line */
        for (int j=0; j<count; j++)
        {
            sum=sum+matrix[min(ROW, line)-j-1][start_col+j];
            if(matrix[min(ROW, line)-j-1][start_col+j]!=0&&!(line==1&&j==0)&&!(line==ROW+COL-1&&j==count-1))
                {
                    p++;
                    r=min(ROW, line)-j-1;
                    c=start_col+j;
                }
        }

        if(p==1&&sum>matrix[r][c]&&sum<SUM)
        {
            Min=1;
        }
    }
}

void CheckSpiral(int matrix[][COL])
{
    int count=0,s=0,i;
    for(i=x;i<ROW-x;i++)
    {
        s=s+matrix[i][x];
        if(matrix[i][x]!=0)
        {
            count++;
        }
    }
    for(i=x;i<ROW-x;i++)
    {
        s=s+matrix[i][COL-x-1];
        if(matrix[i][COL-x-1]!=0)
        {
            count++;
        }
    }
    for(i=x;i<COL-x;i++)
    {
        s=s+matrix[x][i];
        if(matrix[x][i]!=0)
        {
            count++;
        }
    }
    for(i=x;i<COL-x;i++)
    {
        s=s+matrix[ROW-x-1][i];
        if(matrix[ROW-x-1][i]!=0)
        {
            count++;
        }
    }
    x++;
    if(s==2&&count==1)
    {
        Min=1;
    }
    else if(x<ROW/2&&x<COL/2)
    {
        CheckSpiral(matrix);
    }
}
// Driver program to test above functions
int main()
{
    int m,n,i,j;
    scanf("%d%d",&ROW,&COL);
    n=ROW;
    m=COL;
    int a[n][m],b[n][m];
    char s[n][m];
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(j==0)
            {
                scanf(" %c",&s[i][j]);
            }
            else
            {
                scanf("%c",&s[i][j]);
            }

        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            a[i][j]=0;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                if(s[i+1][j]=='#'&&i!=n-1)
                {
                    a[i][j]++;
                }
                if(s[i-1][j]=='#'&&i!=0)
                {
                    a[i][j]++;
                }
                if(s[i][j+1]=='#'&&j!=m-1)
                {
                    a[i][j]++;
                }
                if(s[i][j-1]=='#'&&j!=0)
                {
                    a[i][j]++;
                }
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]<Min&&a[i][j]!=0)
            {
                Min=a[i][j];
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            SUM=SUM+a[i][j];
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            b[i][m-j-1]=a[i][j];
        }
    }
    if(Min!=1)
    {
        diagonalOrder(a);
    }
    if(Min!=1)
    {
        diagonalOrder(b);
    }
    if(Min!=1)
    CheckSpiral(a);
    if(Min!=1)
    {
        for(i=0;i<n;i++)
        {
            int nik=0,hil=0;
            for(j=0;j<m;j++)
            {
                nik=nik+a[i][j];
                if(a[i][j]!=0)
                {
                    hil++;
                }
            }
            if(nik==2&&hil==1)
            {
                Min=1;
                break;
            }
        }
    }
    if(Min!=1)
    {
        for(i=0;i<m;i++)
        {
            int nik=0,hil=0;
            for(j=0;j<n;j++)
            {
                nik=nik+a[j][i];
                if(a[j][i]!=0)
                {
                    hil++;
                }
            }
            if(nik==2&&hil==1)
            {
                Min=1;
                break;
            }
        }
    }
    int count=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                count++;
            }
        }
    }
    if(count==1||count==2)
    {
        Min=-1;
    }
    printf("%d",Min);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
int Min=2,sum=0,SUM=0;
int COL,ROW,x=0,y=0;
int min(int a, int b)
{ return (a < b)? a: b; }

int MIN(int a, int b, int c)
{ return min(min(a, b), c);}

int max(int a, int b)
{ return (a > b)? a: b; }

void diagonalOrder(int matrix[][COL])
{
    // There will be ROW+COL-1 lines in the output
    for (int line=1; line<=(ROW + COL -1); line++)
    {
        int p=0,r,c;
        /* Get column index of the first element in this line of output.
           The index is 0 for first ROW lines and line - ROW for remaining
           lines  */
        int start_col =  max(0, line-ROW);

        /* Get count of elements in this line. The count of elements is
           equal to minimum of line number, COL-start_col and ROW */
         int count = MIN(line, (COL-start_col), ROW);

        /* Print elements of this line */
        for (int j=0; j<count; j++)
        {
            sum=sum+matrix[min(ROW, line)-j-1][start_col+j];
            if(matrix[min(ROW, line)-j-1][start_col+j]!=0&&!(line==1&&j==0)&&!(line==ROW+COL-1&&j==count-1))
                {
                    p++;
                    r=min(ROW, line)-j-1;
                    c=start_col+j;
                }
        }

        if(p==1&&sum>matrix[r][c]&&sum<SUM)
        {
            Min=1;
        }
    }
}
void CheckSpiral(int matrix[][COL])
{
    int count=0,s=0,i;
    for(i=x;i<ROW-x;i++)
    {
        s=s+matrix[i][y];
        if(matrix[i][y]!=0)
        {
            count++;
        }
    }
    for(i=x;i<ROW-x;i++)
    {
        s=s+matrix[i][ROW-y-1];
        if(matrix[i][ROW-y-1]!=0)
        {
            count++;
        }
    }
    for(i=y;i<COL-y;i++)
    {
        s=s+matrix[x][i];
        if(matrix[x][i]!=0)
        {
            count++;
        }
    }
    for(i=y;i<COL-y;i++)
    {
        s=s+matrix[COL-x-1][i];
        if(matrix[COL-x-1][i]!=0)
        {
            count++;
        }
    }
    x++;
    y++;
    if(s==2&&count==1)
    {
        Min=1;
    }
    else if(x<ROW/2&&y<COL/2)
    {
        CheckSpiral(matrix);
    }
}
// Driver program to test above functions
int main()
{
    int m,n,i,j;
    scanf("%d%d",&ROW,&COL);
    n=ROW;
    m=COL;
    int a[n][m],b[n][m];
    char s[n][m];
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(j==0)
            {
                scanf(" %c",&s[i][j]);
            }
            else
            {
                scanf("%c",&s[i][j]);
            }

        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            a[i][j]=0;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                if(s[i+1][j]=='#'&&i!=n-1)
                {
                    a[i][j]++;
                }
                if(s[i-1][j]=='#'&&i!=0)
                {
                    a[i][j]++;
                }
                if(s[i][j+1]=='#'&&j!=m-1)
                {
                    a[i][j]++;
                }
                if(s[i][j-1]=='#'&&j!=0)
                {
                    a[i][j]++;
                }
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]<Min&&a[i][j]!=0)
            {
                Min=a[i][j];
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            SUM=SUM+a[i][j];
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            b[i][m-j-1]=a[i][j];
        }
    }
    if(Min!=1)
    {
        diagonalOrder(a);
    }
    if(Min!=1)
    {
        diagonalOrder(b);
    }
    int count=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                count++;
            }
        }
    }
    CheckSpiral(a);
    if(count==1&&count==2)
    {
        Min=-1;
    }
    printf("%d",Min);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
int Min=2,sum=0,SUM=0;
int COL,ROW;
int min(int a, int b)
{ return (a < b)? a: b; }

int MIN(int a, int b, int c)
{ return min(min(a, b), c);}

int max(int a, int b)
{ return (a > b)? a: b; }

void diagonalOrder(int matrix[][COL])
{
    // There will be ROW+COL-1 lines in the output
    for (int line=1; line<=(ROW + COL -1); line++)
    {
        int p=0,r,c;
        /* Get column index of the first element in this line of output.
           The index is 0 for first ROW lines and line - ROW for remaining
           lines  */
        int start_col =  max(0, line-ROW);

        /* Get count of elements in this line. The count of elements is
           equal to minimum of line number, COL-start_col and ROW */
         int count = MIN(line, (COL-start_col), ROW);

        /* Print elements of this line */
        for (int j=0; j<count; j++)
        {
            sum=sum+matrix[min(ROW, line)-j-1][start_col+j];
            if(matrix[min(ROW, line)-j-1][start_col+j]!=0&&!(line==1&&j==0)&&!(line==ROW+COL-1&&j==count-1))
                {
                    p++;
                    r=min(ROW, line)-j-1;
                    c=start_col+j;
                }
        }

        if(p==1&&sum>matrix[r][c]&&sum<SUM)
        {
            Min=1;
        }
    }
}

// Driver program to test above functions
int main()
{
    int m,n,i,j;
    scanf("%d%d",&ROW,&COL);
    n=ROW;
    m=COL;
    int a[n][m],b[n][m];
    char s[n][m];
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(j==0)
            {
                scanf(" %c",&s[i][j]);
            }
            else
            {
                scanf("%c",&s[i][j]);
            }

        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            a[i][j]=0;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                if(s[i+1][j]=='#'&&i!=n-1)
                {
                    a[i][j]++;
                }
                if(s[i-1][j]=='#'&&i!=0)
                {
                    a[i][j]++;
                }
                if(s[i][j+1]=='#'&&j!=m-1)
                {
                    a[i][j]++;
                }
                if(s[i][j-1]=='#'&&j!=0)
                {
                    a[i][j]++;
                }
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]<Min&&a[i][j]!=0)
            {
                Min=a[i][j];
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            SUM=SUM+a[i][j];
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            b[i][m-j-1]=a[i][j];
        }
    }
    if(Min!=1)
    {
        diagonalOrder(a);
    }
    if(Min!=1)
    {
        diagonalOrder(b);
    }
    int count=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='#')
            {
                count++;
            }
        }
    }
    if(count==1&&count==2)
    {
        Min=-1;
    }
    printf("%d",Min);
    return 0;
}