#include <bits/stdc++.h>
using namespace std;
const int fx[8] = {-1, 1, 0, 0, -1, -1, 1, 1},
          fy[8] = {0, 0, -1, 1, -1, 1, -1, 1};
const int MAX_N = 1000;
int n, m, ans, bcnt, vis[MAX_N + 2][MAX_N + 2];
char buf[MAX_N + 2][MAX_N + 2];
struct node_t {
  int x, y;
  node_t() {}
  node_t(int _x, int _y) { x = _x, y = _y; }
};
node_t op[(MAX_N + 2) * (MAX_N + 2)], q[(MAX_N + 2) * (MAX_N + 2)];
int ops, qh, qt;
bool inr(int x, int y) {
  return (x >= 0 && x <= n + 1 && y >= 0 && y <= m + 1);
}
void dfs(int x, int y) {
  vis[x][y] = bcnt;
  for (int d = 0; d < 4; d++) {
    int xx = x + fx[d], yy = y + fy[d];
    if (inr(xx, yy) && buf[xx][yy] == '1' && vis[xx][yy] == bcnt - 1)
      dfs(xx, yy);
  }
}
int bfs(int x, int y) {
  qh = 0, qt = -1, ops = 0, bcnt++;
  q[++qt] = node_t(x, y), vis[x][y] = 1;
  while (qh <= qt) {
    x = q[qh].x, y = q[qh].y;
    qh++;
    for (int d = 0; d < 8; d++) {
      int xx = x + fx[d], yy = y + fy[d];
      if (!inr(xx, yy)) continue;
      if (buf[xx][yy] == '1' && vis[xx][yy] != bcnt) {
        vis[xx][yy] = bcnt;
        op[ops++] = node_t(xx, yy);
      } else if (buf[xx][yy] == '0' && !vis[xx][yy]) {
        vis[xx][yy] = 1;
        q[++qt] = node_t(xx, yy);
      }
    }
  }
  if (!ops) return 0;
  for (int i = 0; i < ops; i++) {
    int tx = op[i].x, ty = op[i].y, o = 0;
    if (tx == 0 || tx == n + 1 || ty == 0 || ty == m + 1) return 0;
    for (int d = 0; d < 4; d++) {
      int xx = tx + fx[d], yy = ty + fy[d];
      if (inr(xx, yy) && buf[xx][yy] == '1' && vis[xx][yy] == bcnt) o++;
    }
    if (o != 2) return 0;
  }
  bcnt++, dfs(op[0].x, op[0].y);
  for (int i = 0; i < ops; i++)
    if (vis[op[i].x][op[i].y] != bcnt) return 0;
  return ops;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%s", buf[i] + 1);
  for (int i = 0; i <= n + 1; i++) buf[i][0] = buf[i][m + 1] = '1';
  for (int j = 0; j <= m + 1; j++) buf[0][j] = buf[n + 1][j] = '1';
  for (int i = 1; i < n; i++)
    for (int j = 1; j < m; j++)
      if (buf[i][j] == '1' && buf[i][j + 1] == '1' && buf[i + 1][j] == '1' &&
          buf[i + 1][j + 1] == '1')
        ans = 4;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (buf[i][j] == '0' && !vis[i][j]) ans = max(ans, bfs(i, j));
  printf("%d\n", ans);
  return 0;
}
