#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 55;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int n, m;
int start;
int cnt = 0;
char a[MAX_N][MAX_N];
vector<int> g[MAX_N * MAX_N];
bool art = false;
int nxt = 0;
int qqq = 0;
int lvl[MAX_N * MAX_N];
int low[MAX_N * MAX_N];
int mark[MAX_N * MAX_N];
void go(int node, int parent) {
  mark[node] = true;
  lvl[node] = nxt++;
  low[node] = lvl[node];
  for (int i = 0; i < g[node].size(); ++i) {
    int nnode = g[node][i];
    if (nnode == parent) continue;
    if (!mark[nnode]) {
      if (node == start) ++qqq;
      go(nnode, node);
      if (node != start && low[nnode] >= lvl[node]) {
        art = true;
      }
    }
    low[node] = min(low[node], low[nnode]);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) scanf("%s", &a[i]);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      if (a[i][j] == '.') continue;
      ++cnt;
      start = i * m + j;
      for (int dir = 0; dir < 4; ++dir) {
        int ni = i + dx[dir];
        int nj = j + dy[dir];
        if (ni < 0 || ni >= n || nj < 0 || nj >= m) continue;
        if (a[ni][nj] == '.') continue;
        g[i * m + j].push_back(ni * m + nj);
        g[ni * m + nj].push_back(i * m + j);
      }
    }
  if (cnt <= 2)
    cout << -1 << endl;
  else {
    memset(mark, 0, sizeof(mark));
    go(start, -1);
    if (art || qqq > 1)
      cout << 1 << endl;
    else
      cout << 2 << endl;
  }
  return 0;
}
