#include <bits/stdc++.h>
using namespace std;
char mp[1050][1050];
int n, m, stamp = 1, res = 0;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int used[1050][1050];
int used_row[1050];
int R[1050][1050];
int dat[1050][1050];
int cnt;
vector<pair<int, int> > path[1050];
vector<int> row;
void recur(int bx, int by, int bef) {
  int x = bx + dx[bef];
  int y = by + dy[bef];
  if (((int)(path[bx]).size()) != 0 && dat[bx][path[bx][0].first] != stamp)
    path[bx].clear();
  if (used_row[bx] != stamp) row.push_back(bx);
  path[bx].push_back(make_pair(by, bef));
  used_row[bx] = stamp;
  cnt++;
  dat[bx][by] = dat[x][y] = stamp;
  if (used[x][y] == stamp) {
    if (bef < 2) return;
    for (int i = 0; i < ((int)(((int)(row).size()))); ++i) {
      int r = row[i];
      sort((path[r]).begin(), (path[r]).end());
      for (int j = 0; j < ((int)(((int)(path[r]).size()) - 1)); ++j) {
        if (R[r][path[r][j].first] == path[r][j + 1].first) continue;
        if (path[r][j + 0].second != 2) continue;
        if (path[r][j + 1].second != 0) continue;
        return;
      }
      for (int j = 0; j < ((int)(((int)(path[r]).size()))); ++j) {
        int tmp = 0;
        for (int k = 0; k < ((int)(4)); ++k) {
          int xx = r + dx[k];
          int yy = path[r][j].first + dy[k];
          if (xx < 0 || yy < 0 || n <= xx || m <= yy) continue;
          if (dat[xx][yy] == stamp) tmp++;
        }
        if (tmp != 2) return;
      }
    }
    res = max(res, cnt);
    return;
  }
  if (used[bx][by] && bef == 1) return;
  if (bef == 1) used[bx][by] = stamp;
  for (int i = 0; i < ((int)(3)); ++i) {
    int dir = (i + bef + 3) % 4;
    int xx = x + dx[dir];
    int yy = y + dy[dir];
    if (xx < 0 || n <= xx || yy < 0 || m <= yy) continue;
    if (mp[xx][yy] == '0') continue;
    recur(x, y, dir);
    return;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < ((int)(n)); ++i) scanf("%s", mp[i]);
  memset(used, 0, sizeof(used));
  memset(used_row, 0, sizeof(used_row));
  memset(R, -1, sizeof(R));
  for (int i = 0, bef = -1; i < n; i++) {
    for (int j = 0; j < ((int)(m)); ++j)
      if (mp[i][j] == '1') {
        if (bef != -1) R[i][bef] = j;
        bef = j;
      }
  }
  for (int i = 0; i < ((int)(n)); ++i)
    for (int j = 0; j < ((int)(m)); ++j) {
      if (used[i][j] != 0 || mp[i][j] == '0') continue;
      stamp++;
      cnt = 0;
      row.clear();
      int x = i + dx[1];
      int y = j + dy[1];
      if (x < 0 || n <= x || y < 0 || m <= y || mp[x][y] == '0') continue;
      recur(i, j, 1);
    }
  printf("%d\n", res);
}
