#include <bits/stdc++.h>
using namespace std;
int d0[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
int d1[8][2] = {{-1, 0}, {1, 0}, {0, 1},  {0, -1},
                {-1, 1}, {1, 1}, {1, -1}, {-1, -1}};
int n, m;
string t[1001];
int ans = 0;
int b1[1001][1001];
int id = 1;
bool ok;
set<pair<int, int>> lst;
bool valid(int x, int y) { return x >= 0 && y >= 0 && x < n && y < m; }
void dfs1(int x, int y) {
  b1[x][y] = id;
  for (int i = 0; i < 8; ++i) {
    int nx = x + d1[i][0], ny = y + d1[i][1];
    if (valid(nx, ny) && b1[nx][ny] != id && t[nx][ny] == '0') {
      dfs1(nx, ny);
    }
    if (!valid(nx, ny)) ok = false;
  }
  for (int i = 0; i < 8; ++i) {
    int nx = x + d1[i][0], ny = y + d1[i][1];
    if (valid(nx, ny) && t[nx][ny] == '1') {
      lst.insert(make_pair(nx, ny));
    }
  }
}
int bej;
int b2[1001][1001];
void dfs2(int x, int y) {
  int szomsz = 0;
  b2[x][y] = id;
  bej++;
  for (int i = 0; i < 4; ++i) {
    int nx = x + d0[i][0], ny = y + d0[i][1];
    if (valid(nx, ny) && lst.find(make_pair(nx, ny)) != lst.end()) {
      szomsz++;
      if (b2[nx][ny] != id) {
        dfs2(nx, ny);
      }
    }
  }
  ok &= szomsz == 2;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) cin >> t[i];
  for (int i = 0; i + 1 < n; ++i) {
    for (int j = 0; j + 1 < m; ++j) {
      if (t[i][j] == '1' && t[i + 1][j] == '1' && t[i][j + 1] == '1' &&
          t[i + 1][j + 1] == '1')
        ans = 4;
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (b1[i][j] == 0 && t[i][j] == '0') {
        lst.clear();
        ok = true;
        dfs1(i, j);
        bej = 0;
        if (!lst.empty()) {
          dfs2(lst.begin()->first, lst.begin()->second);
          ok &= bej == (int)lst.size();
          if (ok) ans = max(ans, bej);
        }
        id++;
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
