#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int col[maxn][maxn];
char str[maxn][maxn];
int view[maxn][maxn], visit[maxn][maxn], n, m, t, tt, color, stx, sty;
int getmax(int a, int b) { return a > b ? a : b; }
void init() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", str[i]);
}
bool dfs(int x, int y) {
  if (x < 0 || x == n || y < 0 || y == m) return 0;
  if (visit[x][y] != color) {
    if (str[x][y] == '0') {
      visit[x][y] = color;
      str[x][y] = '2';
      int ret = 1;
      for (int i = 0; i < 8; i++) ret &= dfs(x + dx[i], y + dy[i]);
      return ret;
    } else if (str[x][y] == '1') {
      if (col[x][y] != color) {
        col[x][y] = color;
        t++;
      }
      stx = x;
      sty = y;
    }
  }
  return 1;
}
bool dfs2(int x, int y) {
  view[x][y] = color;
  tt++;
  int k = 0;
  if (x > 0) k += (col[x - 1][y] == color);
  if (x < n - 1) k += col[x + 1][y] == color;
  if (y > 0) k += (y > 0 && col[x][y - 1] == color);
  if (y < m - 1) k += col[x][y + 1] == color;
  if (k != 2) return 0;
  int ret = 1;
  if (x > 0 && col[x - 1][y] == color && view[x - 1][y] != color)
    ret &= dfs2(x - 1, y);
  if (x < n - 1 && col[x + 1][y] == color && view[x + 1][y] != color)
    ret &= dfs2(x + 1, y);
  if (y > 0 && col[x][y - 1] == color && view[x][y - 1] != color)
    ret &= dfs2(x, y - 1);
  if (y < m - 1 && col[x][y + 1] == color && view[x][y + 1] != color)
    ret &= dfs2(x, y + 1);
  return ret;
}
int solve() {
  tt = 0;
  if (dfs2(stx, sty)) {
    if (tt == t)
      return t;
    else
      return 0;
  } else
    return 0;
}
void work() {
  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < m - 1; j++) {
      if (str[i][j] == '1' && str[i][j + 1] == '1' && str[i + 1][j] == '1' &&
          str[i + 1][j + 1] == '1')
        ans = 4;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (str[i][j] == '0') {
        color++;
        t = 0;
        if (dfs(i, j)) ans = getmax(ans, solve());
      };
    }
  }
  printf("%d\n", ans);
}
int main() {
  init();
  work();
  return 0;
}
