#include <bits/stdc++.h>
using namespace std;
vector<bool> used;
vector<long long> tin;
vector<long long> up;
vector<vector<long long>> graph;
long long timer = 0;
long long otv = 0;
void dfs2(int v, int parent = -1) {
  used[v] = true;
  tin[v] = up[v] = timer++;
  int children = 0;
  for (int i = 0; i < graph[v].size(); ++i) {
    int u = graph[v][i];
    if (u == parent) {
      continue;
    }
    if (!used[u]) {
      dfs2(u, v);
      up[v] = min(up[v], up[u]);
      ++children;
      if ((parent != -1 && tin[v] <= up[u]) ||
          (parent == -1 && children >= 2)) {
        otv++;
      }
    } else {
      up[v] = min(up[v], tin[u]);
    }
  }
}
void dfs1(long long v, long long parent = -1) {
  used[v] = true;
  tin[v] = up[v] = timer++;
  for (long long i = 0; i < graph[v].size(); ++i) {
    long long u = graph[v][i];
    if (u == parent) {
      continue;
    }
    if (!used[u]) {
      dfs1(u, v);
      up[v] = min(up[v], up[u]);
      if (tin[v] < up[u]) {
        cout << "bridge : " << v + 1 << ' ' << u + 1 << "\n";
      }
    } else {
      up[v] = min(up[v], tin[u]);
    }
  }
}
int main() {
  long long n, m, kolvo = 0;
  cin >> n >> m;
  graph.resize(n * m);
  used.resize(n * m, false);
  tin.resize(n * m);
  up.resize(n * m);
  vector<string> a(n);
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
    for (long long j = 0; j < m; ++j) {
      if (a[i][j] == '#') {
        kolvo++;
        if (i != 0 && a[i - 1][j] == a[i][j]) {
          graph[i * m + j].push_back((i - 1) * m + j);
          graph[(i - 1) * m + j].push_back(i * m + j);
        }
        if (j != 0 && a[i][j - 1] == a[i][j]) {
          graph[i * m + j].push_back(i * m + j - 1);
          graph[i * m + j - 1].push_back(i * m + j);
        }
      }
    }
  }
  long long j;
  if (kolvo <= 2) {
    cout << -1;
    return 0;
  }
  for (long long i = 0; i < n * m; ++i) {
    j = i;
    if (graph[i].size() > 0) {
      break;
    }
  }
  otv = 0;
  dfs2(j);
  if (otv > 0) {
    cout << 1;
  } else {
    cout << 2;
  }
}
