#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1005;
int val[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N];
bool tmp_vis[MAX_N][MAX_N];
int first_i, first_j;
queue<pair<int, int> > ones;
bool all_ok;
void bfs_until_one(int i, int j, int val[MAX_N][MAX_N], bool vis[MAX_N][MAX_N],
                   int n, int m) {
  queue<pair<int, int> > q;
  q.push(make_pair(i, j));
  vis[i][j] = true;
  while (!q.empty()) {
    i = q.front().first;
    j = q.front().second;
    q.pop();
    if ((i == 0 || i == n - 1 || j == 0 || j == m - 1) && val[i][j] == 0)
      all_ok = false;
    for (int di = -1; di <= 1; di++)
      for (int dj = -1; dj <= 1; dj++) {
        if (di == 0 && dj == 0) continue;
        if (val[i][j] == 1) {
          first_i = i;
          first_j = j;
          tmp_vis[i][j] = false;
          ones.push(make_pair(i, j));
          continue;
        }
        int ii = i + di;
        int jj = j + dj;
        if (0 <= ii && ii < n && 0 <= jj && jj < m && !vis[ii][jj]) {
          vis[ii][jj] = true;
          q.push(make_pair(ii, jj));
        }
      }
  }
}
int try_cycle(int val[MAX_N][MAX_N], bool vis[MAX_N][MAX_N], int n, int m) {
  int ans = 0;
  int i = first_i, j = first_j;
  do {
    tmp_vis[i][j] = true;
    ans++;
    vector<pair<int, int> > adj;
    for (int di = -1; di <= 1; di++)
      for (int dj = -1; dj <= 1; dj++) {
        if (di == 0 && dj == 0) continue;
        if (di != 0 && dj != 0) continue;
        int ii = i + di;
        int jj = j + dj;
        if (0 <= ii && ii < n && 0 <= jj && jj < m && val[ii][jj] == 1 &&
            vis[ii][jj])
          adj.push_back(make_pair(ii, jj));
      }
    if ((int)adj.size() != 2) {
      ans = 0;
      break;
    }
    int i1 = adj[0].first, j1 = adj[0].second;
    int i2 = adj[1].first, j2 = adj[1].second;
    if (tmp_vis[i1][j1]) {
      if (tmp_vis[i2][j2]) break;
      i = i2;
      j = j2;
    } else {
      i = i1;
      j = j1;
    }
  } while (i != first_i || j != first_j);
  while (!ones.empty()) {
    i = ones.front().first;
    j = ones.front().second;
    ones.pop();
    vis[i][j] = false;
    if (!tmp_vis[i][j]) ans = 0;
  }
  return ans;
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      char c;
      scanf(" %c", &c);
      val[i][j] = (c == '1');
    }
  int ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (val[i][j] == 0 && !vis[i][j]) {
        all_ok = true;
        bfs_until_one(i, j, val, vis, n, m);
        int res = try_cycle(val, vis, n, m);
        if (all_ok && res > ans) ans = res;
      }
  if (min(n, m) >= 2) {
    for (int i = 0; i < n - 1; i++)
      for (int j = 0; j < m - 1; j++) {
        if (val[i][j] == 1 && val[i + 1][j] == 1 && val[i][j + 1] == 1 &&
            val[i + 1][j + 1] == 1) {
          ans = max(ans, 4);
          goto AFTER;
        }
      }
  AFTER:;
  }
  printf("%d\n", ans);
  return 0;
}
