#include <bits/stdc++.h>
using namespace std;
int n, m;
int i, j, k, l;
char s[1003][1003];
int u[1003][1003];
int v[1003][1003];
bool w[1003][1003];
int c;
bool ok;
int xc[1000006], yc[1000006], d;
int ans;
void dfs1(int x, int y) {
  u[x][y] = c;
  int i, j;
  for (i = -1; i < 2; i += 2)
    for (j = -1; j < 2; j += 2)
      if (s[x + i][y] == '1' && s[x][y + j] == '1' && s[x + i][y + j] == '1' &&
          u[x + i][y + j] != c) {
        xc[d] = x + i;
        yc[d] = y + j;
        ++d;
        u[x + i][y + j] = c;
      }
  for (i = -1; i < 2; ++i)
    for (j = -1; j < 2; ++j)
      if (i || j) {
        int ox = x + i, oy = y + j;
        if (s[ox][oy] == '1' && u[ox][oy] != c) {
          xc[d] = ox;
          yc[d] = oy;
          ++d;
          u[ox][oy] = c;
        }
        if (!u[ox][oy] && s[ox][oy] == '0') dfs1(ox, oy);
      }
}
void dfs_f(int x, int y) {
  v[x][y] = c;
  int deg = 0;
  for (int i = -1; i < 2; ++i)
    for (int j = -1; j < 2; ++j)
      if ((i == 0) != (j == 0)) {
        int ox = x + i, oy = y + j;
        if (u[ox][oy] == c && s[ox][oy] == '1') {
          if (v[ox][oy] != c) dfs_f(ox, oy);
          ++deg;
        }
      }
  if (deg != 2) ok = false;
}
void dfs_gr(int x, int y) {
  w[x][y] = 1;
  for (int i = -1; i < 2; ++i)
    for (int j = -1; j < 2; ++j)
      if ((i == 0) != (j == 0)) {
        int ox = x + i, oy = y + j;
        if (s[ox][oy] == '0' && !w[ox][oy]) dfs_gr(ox, oy);
      }
}
int main() {
  cin >> n >> m;
  gets(s[0]);
  for (i = 1; i <= n; ++i) gets(s[i] + 1);
  for (i = 1; i <= n; ++i) {
    if (s[i][1] == '0') dfs_gr(i, 1);
    if (s[i][m] == '0') dfs_gr(i, m);
  }
  for (j = 1; j <= m; ++j) {
    if (s[1][j] == '0') dfs_gr(1, j);
    if (s[n][j] == '0') dfs_gr(n, j);
  }
  ans = 0;
  bool run = false;
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= m; ++j)
      if (!w[i][j] && s[i][j] == '0' && !u[i][j]) {
        ++c;
        d = 0;
        dfs1(i, j);
        if (d) {
          ok = true;
          dfs_f(xc[0], yc[0]);
          for (k = 0; k < d; ++k)
            if (v[xc[k]][yc[k]] != c) ok = false;
          if (ok) ans = ((ans) > (d) ? (ans) : (d));
        }
      } else if (s[i][j] == '1' && s[i + 1][j] == '1' && s[i][j + 1] == '1' &&
                 s[i + 1][j + 1] == '1')
        ans = ((ans) > (4) ? (ans) : (4));
  cout << ans << endl;
  return 0;
}
