#include <bits/stdc++.h>
using namespace std;
const int mr[] = {0, 1, 0, -1, -1, 1, -1, 1};
const int mc[] = {1, 0, -1, 0, -1, 1, 1, -1};
const int INF = 1000000000;
char a[1002][1002];
bool v[1002][1002];
bool u[1002][1002];
vector<int> vr, vc;
void flood(int r, int c) {
  a[r][c] = '#';
  for (int o = 0; o < 8; o++) {
    int dr = r + mr[o];
    int dc = c + mc[o];
    if (!v[dr][dc]) {
      v[dr][dc] = true;
      vr.push_back(dr);
      vc.push_back(dc);
    }
    if (a[dr][dc] == '0') flood(dr, dc);
  }
}
int goborder(int r, int c) {
  u[r][c] = true;
  int cnt = 0, ret = 1;
  for (int o = 0; o < 4; o++) {
    int dr = r + mr[o];
    int dc = c + mc[o];
    if (v[dr][dc]) cnt++;
  }
  if (cnt != 2) return 0;
  for (int o = 0; o < 4; o++) {
    int dr = r + mr[o];
    int dc = c + mc[o];
    if (u[dr][dc] || !v[dr][dc]) continue;
    int sz = goborder(dr, dc);
    if (!sz)
      return 0;
    else
      ret += sz;
  }
  return ret;
}
int gao(int r, int c) {
  vr.clear();
  vc.clear();
  flood(r, c);
  int n = vr.size();
  bool vout = false;
  for (int i = 0; i < n; i++) {
    if (!a[vr[i]][vc[i]]) vout = true;
    v[vr[i]][vc[i]] = false;
  }
  if (vout) return 0;
  vector<int> br, bc;
  for (int i = 0; i < n; i++)
    if (a[vr[i]][vc[i]] != '#') {
      br.push_back(vr[i]);
      bc.push_back(vc[i]);
      v[vr[i]][vc[i]] = true;
    }
  int ret = goborder(br[0], bc[0]);
  int m = br.size();
  for (int i = 0; i < m; i++) {
    v[br[i]][bc[i]] = false;
    u[br[i]][bc[i]] = false;
  }
  if (ret != m)
    return 0;
  else
    return ret;
}
int main() {
  int n, m, ans = 0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%s", a[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] == '0') ans = max(ans, gao(i, j));
  for (int i = 2; i <= n; i++)
    for (int j = 2; j <= m; j++) {
      if (a[i][j] == '1' && a[i - 1][j - 1] == '1' && a[i][j - 1] == '1' &&
          a[i - 1][j] == '1')
        ans = max(ans, 4);
    }
  printf("%d\n", ans);
}
