#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[51][51];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
queue<pair<int, int> > q;
int br = 0;
bool moze(int x, int y, int k) {
  if (x < 0 || y < 0 || x >= n || y >= m || a[x][y] == 0 || a[x][y] == k)
    return false;
  return true;
}
void bfs(int l, int r, int k) {
  br = 0;
  a[l][r] = k;
  queue<pair<int, int> > p;
  br++;
  p.push(make_pair(l, r));
  while (p.size()) {
    int x = p.front().first;
    int y = p.front().second;
    p.pop();
    for (int i = 0; i < 4; i++) {
      int x1 = x + dx[i];
      int y1 = y + dy[i];
      if (moze(x1, y1, k)) {
        br++;
        a[x1][y1] = k;
        p.push(make_pair(x1, y1));
      }
    }
  }
}
int uk = 0;
bool most(int l, int r, int k, int x, int y) {
  a[l][r] = k;
  bfs(x, y, k);
  if (uk == br + 1) return false;
  return true;
}
int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      if (c == '.') {
        a[i][j] = 0;
      } else {
        uk++;
        a[i][j] = 1;
        q.push(make_pair(i, j));
      }
    }
  }
  bfs(q.front().first, q.front().second, 2);
  if (br != q.size()) {
    cout << 0;
    return 0;
  }
  if (q.size() < 2) {
    cout << -1;
    return 0;
  }
  pair<int, int> pi = q.front();
  int k = 4;
  q.pop();
  int x = pi.first;
  int y = pi.second;
  if (most(pi.first, pi.second, k, q.front().first, q.front().second)) {
    cout << 1;
    return 0;
  } else {
    k++;
    while (q.size()) {
      if (most(q.front().first, q.front().second, k, x, y)) {
        cout << 1;
        return 0;
      }
      k++;
      q.pop();
    }
  }
  if (uk <= 3) {
    cout << -1;
    return 0;
  }
  cout << 2;
  return 0;
}
