#include <bits/stdc++.h>
using namespace std;
int n, m;
int A[1047][1047];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < (n + 4); i++) A[i][0] = A[i][m + 3] = -1;
  for (int i = 0; i < (m + 4); i++) A[0][i] = A[n + 3][i] = -1;
  for (int i = 1; i < n + 3; i++) A[i][1] = A[i][m + 2] = 0;
  for (int i = 1; i < m + 3; i++) A[1][i] = A[n + 2][i] = 0;
  for (int i = 0; i < (n); i++)
    for (int j = 0; j < (m); j++) {
      char x;
      scanf(" %c", &x);
      A[i + 2][j + 2] = x - '0';
    }
  queue<int> Q;
  Q.push(1);
  Q.push(1);
  A[1][1] = -1;
  while (!Q.empty()) {
    int y = Q.front();
    Q.pop();
    int x = Q.front();
    Q.pop();
    int dx[] = {-1, 0, 1, 1, 1, 0, -1, -1};
    int dy[] = {-1, -1, -1, 0, 1, 1, 1, 0};
    for (int i = 0; i < (8); i++) {
      int y1 = y + dy[i], x1 = x + dx[i];
      if (A[y1][x1] != 0) continue;
      Q.push(y1);
      Q.push(x1);
      A[y1][x1] = -1;
    }
  }
  int res = 0;
  int p = -1;
  for (int i = 2; i < n + 2; i++)
    for (int j = 2; j < m + 2; j++) {
      if (A[i][j] != 0) continue;
      p--;
      int dlz = 0;
      bool ok = true;
      int zy = -1, zx;
      Q.push(i);
      Q.push(j);
      A[i][j] = p;
      while (!Q.empty()) {
        int y = Q.front();
        Q.pop();
        int x = Q.front();
        Q.pop();
        int dx[] = {-1, 0, 1, 1, 1, 0, -1, -1};
        int dy[] = {-1, -1, -1, 0, 1, 1, 1, 0};
        for (int k = 0; k < (8); k++) {
          int y1 = y + dy[k], x1 = x + dx[k];
          if (A[y1][x1] > 0) {
            if (A[y1][x1] != -p) {
              A[y1][x1] = -p;
              dlz++;
              if (zy == -1) {
                zy = y1;
                zx = x1;
              }
            }
          }
          if (A[y1][x1] != 0) continue;
          Q.push(y1);
          Q.push(x1);
          A[y1][x1] = p;
        }
      }
      int dlz1 = 0;
      int kdey = zy, kdex = zx;
      int posy = -1, posx = -1;
      do {
        dlz1++;
        int dx[] = {0, 0, -1, 1};
        int dy[] = {1, -1, 0, 0};
        int p1 = 0;
        for (int i1 = 0; i1 < (4); i1++) {
          if (A[kdey + dy[i1]][kdex + dx[i1]] == -p) p1++;
        }
        if (p1 != 2) {
          ok = false;
          break;
        }
        for (int i1 = 0; i1 < (4); i1++) {
          if (A[kdey + dy[i1]][kdex + dx[i1]] != -p) continue;
          if (kdey + dy[i1] == posy && kdex + dx[i1] == posx) continue;
          posy = kdey;
          posx = kdex;
          kdey = kdey + dy[i1];
          kdex = kdex + dx[i1];
          break;
        }
      } while (kdey != zy || kdex != zx);
      if (dlz1 != dlz) ok = false;
      if (ok) res = max(res, dlz);
    }
  for (int i = 2; i < n + 2; i++)
    for (int j = 2; j < m + 2; j++) {
      if (A[i][j] > 0 && A[i + 1][j] > 0 && A[i + 1][j + 1] > 0 &&
          A[i][j + 1] > 0)
        res = max(res, 4);
    }
  printf("%d\n", res);
  return 0;
}
