#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : (-x);
}
template <class T>
T sqr(T x) {
  return x * x;
}
const int maxn = 1010;
int n, m;
char a[maxn][maxn];
int u[maxn][maxn];
int IT = 57;
vector<pair<int, int> > cur;
const int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1};
const int dy[] = {0, 0, -1, 1, -1, 1, -1, 1};
bool good(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
bool canOut;
void dfs(int x, int y, int D, bool add) {
  int rev = u[x][y];
  u[x][y] = IT;
  for (int dir = 0; dir < D; ++dir) {
    int xx = x + dx[dir];
    int yy = y + dy[dir];
    if (!good(xx, yy)) {
      canOut = true;
      continue;
    }
    if (a[xx][yy] != a[x][y]) {
      if (add) {
        if (u[xx][yy] != IT) {
          u[xx][yy] = IT;
          cur.push_back(pair<int, int>(xx, yy));
        }
      }
      continue;
    }
    if (u[xx][yy] != rev) continue;
    dfs(xx, yy, D, add);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) scanf("%s", a[i]);
  memset(u, 0, sizeof(u));
  int res = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (a[i][j] == '0' && !u[i][j]) {
        cur.clear();
        ++IT;
        canOut = false;
        dfs(i, j, 8, true);
        if (canOut) continue;
        int rev = IT;
        if (((int)(cur).size()) <= res) continue;
        bool ok = true;
        for (int p = 0; p < ((int)(cur).size()); ++p) {
          int x = cur[p].first;
          int y = cur[p].second;
          int cnt = 0;
          for (int dir = 0; dir < 4; ++dir) {
            int xx = x + dx[dir];
            int yy = y + dy[dir];
            if (!good(xx, yy)) continue;
            if (a[xx][yy] != '1') continue;
            if (u[xx][yy] != rev) continue;
            ++cnt;
          }
          if (cnt != 2) {
            ok = false;
            break;
          }
        }
        if (!ok) continue;
        int cmp = 0;
        ++IT;
        for (int p = 0; p < ((int)(cur).size()); ++p) {
          int x = cur[p].first;
          int y = cur[p].second;
          if (u[x][y] != rev) continue;
          dfs(x, y, 4, false);
          ++cmp;
        }
        if (cmp != 1) continue;
        res = max(res, ((int)(cur).size()));
      }
  for (int i = 0; i + 1 < n; ++i)
    for (int j = 0; j + 1 < m; ++j)
      if (a[i][j] == '1' && a[i + 1][j] == '1' && a[i][j + 1] == '1' &&
          a[i + 1][j + 1] == '1')
        res = max(res, 4);
  cout << res << "\n";
  return 0;
}
