#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000;
const int INF = 1000000001;
char tab[MAX][MAX + 1];
int vis[MAX][MAX];
int n, m;
int dfs(int x, int y) {
  vis[x][y] = 0;
  int s = 1;
  for (int i = x - 1; i <= x + 1; ++i)
    for (int j = y - 1; j <= y + 1; ++j)
      if ((0 <= i && i < n && 0 <= j && j < m) && vis[i][j] == 1 &&
          abs(x - i) + abs(y - j) == 1) {
        s += dfs(i, j);
        return s;
      }
  return s;
}
int solve(int x, int y) {
  queue<pair<int, int> > Q;
  Q.push(make_pair(x, y));
  vis[x][y] = -1;
  vector<pair<int, int> > V;
  bool z = false;
  while (!Q.empty()) {
    pair<int, int> p = Q.front();
    Q.pop();
    for (int i = p.first - 1; i <= p.first + 1; ++i)
      for (int j = p.second - 1; j <= p.second + 1; ++j)
        if ((0 <= i && i < n && 0 <= j && j < m) && !vis[i][j]) {
          if (tab[i][j] == '0') {
            Q.push(make_pair(i, j));
            vis[i][j] = -1;
          } else {
            V.push_back(make_pair(i, j));
            vis[i][j] = 1;
          }
        } else if (!(0 <= i && i < n && 0 <= j && j < m))
          z = true;
  }
  if (V.empty()) return 0;
  for (__typeof((V).begin()) pos = (V).begin(); pos != (V).end(); ++pos) {
    int nb = 0;
    for (int i = pos->first - 1; i <= pos->first + 1; ++i)
      for (int j = pos->second - 1; j <= pos->second + 1; ++j)
        if ((0 <= i && i < n && 0 <= j && j < m) &&
            abs(pos->first - i) + abs(pos->second - j) == 1 && vis[i][j] == 1)
          ++nb;
    if (nb != 2) z = true;
  }
  int s = dfs(V[0].first, V[0].second);
  for (__typeof((V).begin()) it = (V).begin(); it != (V).end(); ++it)
    vis[it->first][it->second] = 0;
  if (!z && s == V.size()) return s;
  return 0;
}
int main(int argc, char *argv[]) {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) scanf("%s", tab[i]);
  int res = 0;
  for (int i = 0; i < n - 1; ++i)
    for (int j = 0; j < m - 1; ++j)
      if (tab[i][j] == '1' && tab[i][j + 1] == '1' && tab[i + 1][j] == '1' &&
          tab[i + 1][j + 1] == '1')
        res = 4;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (tab[i][j] == '0' && !vis[i][j]) res = max(res, solve(i, j));
  printf("%d\n", res);
  return 0;
}
