#include <bits/stdc++.h>
using namespace std;
long long vx[4] = {1, 0, -1, 0};
long long vy[4] = {0, 1, 0, -1};
int vis[1000][1000];
int main() {
  vector<string> field;
  int n, m, ans = 0, cnt = 2;
  cin >> n >> m;
  for (long long i = 0; i < ((long long)n); i++) {
    string str;
    cin >> str;
    field.push_back(str);
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < m - 1; j++) {
      if (field[i][j] == '1' && field[i + 1][j] == '1' &&
          field[i][j + 1] == '1' && field[i + 1][j + 1] == '1') {
        ans = 4;
        break;
      }
    }
    if (ans > 0) break;
  }
  for (long long i = 0; i < ((long long)n); i++)
    for (long long j = 0; j < ((long long)m); j++) vis[i][j] = 0;
  for (long long i = 0; i < ((long long)n); i++)
    for (long long j = 0; j < ((long long)m); j++) {
      if (vis[i][j] != 0 || field[i][j] == '1') continue;
      queue<pair<long long, long long> > bfs;
      bfs.push(make_pair(i, j));
      int len = 0, px = -1, py = -1, coner = 0;
      while (!bfs.empty()) {
        int x = bfs.front().first, y = bfs.front().second;
        bfs.pop();
        if (field[x][y] == '0') {
          if (x == 0 || y == 0 || x == n - 1 || y == m - 1) coner = 1;
          if (vis[x][y]) continue;
          vis[x][y] = 1;
          for (int p = -1; p <= 1; p++) {
            for (int q = -1; q <= 1; q++) {
              if (0 <= x + p && x + p < n && 0 <= y + q && y + q < m)
                bfs.push(make_pair(x + p, y + q));
            }
          }
        } else {
          if (vis[x][y] != cnt) {
            vis[x][y] = cnt;
            len++;
            px = x;
            py = y;
          }
        }
      }
      queue<pair<long long, long long> > cyc;
      int ok = 1, visc = 0;
      if (px >= 0 && coner == 0) cyc.push(make_pair(px, py));
      while (!cyc.empty()) {
        int nx = cyc.front().first, ny = cyc.front().second;
        cyc.pop();
        if (vis[nx][ny] != cnt) continue;
        vis[nx][ny] = -cnt;
        visc++;
        int adj = 0;
        for (int p = 0; p < 4; p++) {
          int nnx = nx + vx[p], nny = ny + vy[p];
          if (0 <= nnx && nnx < n && 0 <= nny && nny < m)
            if (field[nnx][nny] == '1' && abs(vis[nnx][nny]) == cnt) adj++;
        }
        if (adj != 2) {
          ok = 0;
          break;
        } else {
          for (int p = 0; p < 4; p++) {
            int nnx = nx + vx[p], nny = ny + vy[p];
            if (0 <= nnx && nnx < n && 0 <= nny && nny < m)
              if (field[nnx][nny] == '1' && vis[nnx][nny] == cnt)
                cyc.push(make_pair(nx + vx[p], ny + vy[p]));
          }
        }
      }
      if (ok && len == visc) {
        ans = max(ans, len);
      }
      cnt++;
    }
  cout << ans << endl;
  return 0;
}
