#include <bits/stdc++.h>
using namespace ::std;
const int N = 1000;
int n, m, ans, num = -1;
int a[N + 5][N + 5];
int b[N + 5][N + 5];
int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dx2[4] = {1, 1, -1, -1};
int dy2[4] = {1, -1, -1, 1};
void input() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    char in[N + 5];
    scanf("%s", &in[1]);
    for (int j = 1; j <= m; j++) {
      a[i][j] = in[j] - '0';
    }
  }
  if (n >= 2 && m >= 2) {
    for (int i = 1; i < n; i++)
      for (int j = 1; j < m; j++) {
        if (a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1] == 4) ans = 4;
      }
  }
}
void proc(int sx, int sy) {
  bool posi = true;
  a[sx][sy] = num;
  queue<pair<int, int> > q;
  vector<pair<int, int> > v;
  q.push(pair<int, int>(sx, sy));
  a[sx][sy] = num;
  while (!q.empty()) {
    pair<int, int> now = q.front();
    q.pop();
    for (int i = 0; i < 8; i++) {
      pair<int, int> next =
          pair<int, int>(now.first + dx[i], now.second + dy[i]);
      if (next.first == 0 || next.first == n + 1 || next.second == 0 ||
          next.second == m + 1)
        posi = false;
      else if (a[next.first][next.second] == 0) {
        a[next.first][next.second] = num;
        q.push(next);
      } else if (a[next.first][next.second] > 0 &&
                 a[next.first][next.second] != -num) {
        a[next.first][next.second] = -num;
        v.push_back(next);
      }
    }
  }
  if (!posi) return;
  for (int i = 0; i < v.size(); i++) {
    int cnt = 0;
    for (int j = 0; j < 4; j++) {
      if (a[v[i].first + dx[j]][v[i].second + dy[j]] == -num) cnt++;
    }
    if (cnt != 2) posi = false;
  }
  if (!posi) return;
  q.push(v[0]);
  b[v[0].first][v[0].second] = num;
  int cnt = 0;
  while (!q.empty()) {
    cnt++;
    pair<int, int> now = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      pair<int, int> next =
          pair<int, int>(now.first + dx[i], now.second + dy[i]);
      if (b[next.first][next.second] != num &&
          a[next.first][next.second] == -num) {
        b[next.first][next.second] = num;
        q.push(next);
      }
    }
  }
  if (cnt != v.size()) return;
  ans = max(ans, (int)v.size());
}
int main() {
  input();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] == 0) {
        num--;
        proc(i, j);
      }
  printf("%d", ans);
  return 0;
}
