#include <bits/stdc++.h>
using namespace std;
int n, m;
bool used[1001][1001];
int last = 1;
int d[1001][1001];
int dxy[8][2] = {{0, 1},   {1, 0}, {-1, 0}, {0, -1},
                 {-1, -1}, {1, 1}, {-1, 1}, {1, -1}};
int tx, ty, tcnt, ttcnt;
void mark(int x, int y, int clr) {
  used[x][y] = true;
  for (int i = 0; i < 8; ++i) {
    int nx = x + dxy[i][0], ny = y + dxy[i][1];
    if (nx >= 0 && ny >= 0 && nx < n && ny < m) {
      if (d[nx][ny] == 0 && !used[nx][ny])
        mark(nx, ny, clr);
      else if (d[nx][ny] > 0 && d[nx][ny] < clr) {
        tcnt++;
        if (tx == -1) tx = nx, ty = ny;
        d[nx][ny] = clr;
      }
    } else
      tx = -2;
  }
}
bool check(int x, int y, int clr) {
  d[x][y]++;
  tcnt--;
  bool good = true;
  int cnt = 0;
  for (int i = 0; i < 4 && good; ++i) {
    int nx = x + dxy[i][0], ny = y + dxy[i][1];
    if (nx >= 0 && ny >= 0 && nx < n && ny < m) {
      if (d[nx][ny] >= clr) cnt++;
      if (d[nx][ny] == clr) good = check(nx, ny, clr);
    }
  }
  if (cnt != 2) good = false;
  if (tx == x && ty == y && tcnt != 0) good = false;
  return good;
}
int main() {
  cin >> n >> m;
  char tch;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      cin >> tch;
      if (tch == '1')
        d[i][j] = 1;
      else
        d[i][j] = 0;
    }
  int ans = 0;
  for (int i = 0; i < n - 1 && ans == 0; ++i)
    for (int j = 0; j < m - 1; ++j)
      if (d[i][j] == 1 && d[i + 1][j] == 1 && d[i][j + 1] == 1 &&
          d[i + 1][j + 1] == 1) {
        ans = 4;
        break;
      }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (!used[i][j] && d[i][j] == 0) {
        tx = ty = -1, tcnt = 0;
        mark(i, j, ++last);
        if (tx < 0) continue;
        ttcnt = tcnt;
        if (check(tx, ty, last)) ans = max(ans, ttcnt);
        ++last;
      }
  cout << ans;
  return 0;
}
