#include <bits/stdc++.h>
using namespace std;
int n, m, B[1010][1010];
int D[1010][1010], K;
int Deg[1010][1010];
char A[1010][1010];
vector<pair<int, int> > V;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int R, C;
bool cango(int r, int c) {
  return (r >= 0 && r < n && c >= 0 && c < m && (!B[r][c]) && (A[r][c] == '0'));
}
bool ones(int r, int c) {
  return (r >= 0 && r < n && c >= 0 && c < m && (D[r][c] != K) &&
          (A[r][c] == '1'));
}
void dfs(int r, int c) {
  B[r][c] = 1;
  if (r < R) {
    R = r;
    C = c;
  }
  for (int dx = -1; dx <= 1; dx++)
    for (int dy = -1; dy <= 1; dy++) {
      if (ones(r + dx, c + dy)) {
        D[r + dx][c + dy] = K;
        V.push_back(make_pair(r + dx, c + dy));
      }
      if (cango(r + dx, c + dy)) dfs(r + dx, c + dy);
    }
}
bool required(int r, int c) {
  return (r >= 0 && r < n && c >= 0 && c < m && (D[r][c] == K));
}
bool cycle() {
  if (V.size() == 0) return false;
  int k;
  for (int i = 0; i < V.size(); i++) {
    k = 0;
    for (int j = 0; j < 4; j++)
      if (required(V[i].first + dx[j], V[i].second + dy[j])) k++;
    if (k != 2) return false;
    Deg[V[i].first][V[i].second] = 2;
  }
  int r = V[0].first, c = V[0].second, cnt = 0, go = 1;
  while (go) {
    cnt++;
    go = 0;
    for (int j = 0; j < 4; j++)
      if (required(r + dx[j], c + dy[j])) {
        D[r][c] = 0;
        Deg[r][c]--;
        r += dx[j];
        c += dy[j];
        Deg[r][c]--;
        go = 1;
        break;
      }
  }
  return (cnt == (int)V.size());
}
int main() {
  int res = 0;
  cin >> n >> m;
  cin.getline(A[0], 1010, '\n');
  for (int i = 0; i < n; i++) cin.getline(A[i], 1010, '\n');
  K = 1000000000;
  for (int i = 0; i < n && (!res); i++)
    for (int j = 0; j < m && (!res); j++)
      if (ones(i, j) && ones(i, j + 1) && ones(i + 1, j) && ones(i + 1, j + 1))
        res = 4;
  K = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (A[i][j] == '0' && (!B[i][j])) {
        K++;
        V.clear();
        R = 1000000000;
        dfs(i, j);
        if (required(R - 1, C) && cycle()) res = max(res, (int)V.size());
      }
  cout << res << endl;
  return 0;
}
