#include <bits/stdc++.h>
using namespace std;
const int N = 1200;
int n, m, a[N][N], used[N][N], gused[N * N];
char s[N];
const int dx[8] = {1, 0, 0, -1, 1, 1, -1, -1};
const int dy[8] = {0, 1, -1, 0, 1, -1, 1, -1};
int step = 0;
bool can(int x, int y) { return x >= 0 && y >= 0 && x < n && y < m; }
vector<pair<int, int> > t;
bool ok;
void dfs(int x, int y) {
  used[x][y] = true;
  for (int dir = 0; dir < int(8); dir++) {
    int nx = x + dx[dir], ny = y + dy[dir];
    if (can(nx, ny)) {
      if (a[nx][ny])
        t.push_back(make_pair(nx, ny));
      else if (!used[nx][ny])
        dfs(nx, ny);
    } else
      ok = false;
  }
}
vector<vector<int> > g;
void dfs1(int v) {
  gused[v] = step;
  for (int i = 0; i < int(int((g[v]).size())); i++) {
    int to = g[v][i];
    if (gused[to] != step) {
      dfs1(to);
    }
  }
}
int main() {
  cin >> n >> m;
  gets(s);
  for (int i = 0; i < int(n); i++) {
    gets(s);
    for (int j = 0; j < int(m); j++)
      if (s[j] == '1') a[i][j] = 1;
  }
  int res = 0;
  for (int i = 0; i < int(n - 1); i++)
    for (int j = 0; j < int(m - 1); j++) {
      if (a[i][j] && a[i + 1][j] && a[i][j + 1] && a[i + 1][j + 1]) res = 4;
    }
  for (int i = 0; i < int(n); i++)
    for (int j = 0; j < int(m); j++) {
      if (a[i][j] == 0 && !used[i][j]) {
        t.clear();
        ok = true;
        dfs(i, j);
        if (!can) continue;
        if (t.empty()) continue;
        sort((t).begin(), (t).end());
        t.erase(unique((t).begin(), (t).end()), t.end());
        g.clear();
        g.resize(int((t).size()));
        for (int k = 0; k < int(int((t).size())); k++) {
          int x = t[k].first, y = t[k].second;
          for (int dir = 0; dir < int(4); dir++) {
            int nx = x + dx[dir], ny = y + dy[dir];
            int pos = lower_bound((t).begin(), (t).end(), make_pair(nx, ny)) -
                      t.begin();
            if (pos != int((t).size()) && t[pos] == make_pair(nx, ny))
              g[k].push_back(pos);
          }
        }
        for (int k = 0; k < int(int((t).size())); k++) {
          if (int((g[k]).size()) != 2) {
            ok = false;
            break;
          }
        }
        step++;
        dfs1(0);
        for (int k = 0; k < int(int((t).size())); k++) {
          if (gused[k] != step) {
            ok = false;
            break;
          }
        }
        if (ok) res = max(res, int((t).size()));
      }
    }
  cout << res << endl;
}
