#include <bits/stdc++.h>
using namespace std;
const int mr[] = {0, 1, 0, -1};
const int mc[] = {-1, 0, 1, 0};
vector<pair<int, int> > u;
bool v[1005][1005], ok;
char a[1005][1005];
void flood(int r, int c) {
  a[r][c] = '#';
  for (int i = -1; i <= 1; i++)
    for (int j = -1; j <= 1; j++) {
      ok &= a[r + i][c + j] != 0;
      if (a[r + i][c + j] == '0') flood(r + i, c + j);
      if (a[r + i][c + j] == '1' && !v[r + i][c + j]) {
        v[r + i][c + j] = true;
        u.push_back({r + i, c + j});
      }
    }
}
int check(int r, int c) {
  int cnt = 1;
  v[r][c] = false;
  for (int o = 0; o < 4; o++) {
    int dr = r + mr[o], dc = c + mc[o];
    if (v[dr][dc]) cnt += check(dr, dc);
  }
  return cnt;
}
int gao(int r, int c) {
  ok = true;
  flood(r, c);
  int n = u.size();
  for (int i = 0; i < n && ok; i++) {
    r = u[i].first;
    c = u[i].second;
    int cnt = 0;
    for (int o = 0; o < 4; o++)
      if (v[r + mr[o]][c + mc[o]]) cnt++;
    if (cnt != 2) ok = false;
  }
  u.clear();
  if (ok && check(r, c) == n) return n;
  for (int i = 0; i < n; i++) v[u[i].first][u[i].second] = false;
  return 0;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%s", a[i] + 1);
  int ans = 0;
  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));
      if (a[i][j] == '1' && a[i - 1][j - 1] == '1' && a[i - 1][j] == '1' &&
          a[i][j - 1] == '1')
        ans = max(ans, 4);
    }
  printf("%d\n", ans);
}
