#include <bits/stdc++.h>
using namespace std;
long long convertToNum(string s) {
  long long val = 0;
  for (int i = 0; i < (s.size()); i++) val = val * 10 + s[i] - '0';
  return val;
}
string convertToString(long long a) {
  string res = "";
  if (!a) return "0";
  while (a) {
    res = (char)(a % 10 + 48) + res;
    a /= 10;
  }
  return res;
}
long long GCD(long long x, long long y) {
  if (!x) return y;
  if (!y) return x;
  if (x == y) return x;
  if (x < y)
    return GCD(x, y % x);
  else
    return GCD(x % y, y);
}
long long POW(long long x, long long y, long long Base) {
  if (!y) return 1;
  long long u = POW(x, y / 2, Base);
  u = (u * u) % Base;
  if (y & 1)
    return (u * x) % Base;
  else
    return u;
}
int m, n, res;
char s[10100];
char a[1100][1100];
const int dx[8] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
const int dx2[4] = {-1, 1, 1, -1};
const int dy2[4] = {1, 1, -1, -1};
int dd[1100][1100];
int sl[1100][1100];
queue<pair<int, int> > q;
vector<pair<int, int> > list0;
vector<pair<int, int> > list1;
bool notSol;
int TEST;
void bfs(int x, int y) {
  while (!q.empty()) q.pop();
  dd[x][y] = TEST;
  list0.clear();
  list1.clear();
  list0.push_back(make_pair(x, y));
  q.push(make_pair(x, y));
  while (!q.empty()) {
    pair<int, int> tmp = q.front();
    q.pop();
    int u = tmp.first;
    int v = tmp.second;
    for (int k = 0; k < (8); k++)
      if (u + dx[k] > 0 && u + dx[k] <= m && v + dy[k] > 0 && v + dy[k] <= n) {
        if (a[u + dx[k]][v + dy[k]] == '0') {
          if (dd[u + dx[k]][v + dy[k]] != TEST) {
            dd[u + dx[k]][v + dy[k]] = TEST;
            q.push(make_pair(u + dx[k], v + dy[k]));
            list0.push_back(make_pair(u + dx[k], v + dy[k]));
          }
        } else {
          if (dd[u + dx[k]][v + dy[k]] != TEST) {
            dd[u + dx[k]][v + dy[k]] = TEST;
            if (k <= 3)
              sl[u + dx[k]][v + dy[k]] = 1;
            else
              sl[u + dx[k]][v + dy[k]] = 0;
            list1.push_back(make_pair(u + dx[k], v + dy[k]));
          } else {
            if (0 <= k && k <= 3) {
              sl[u + dx[k]][v + dy[k]]++;
            }
            if (sl[u + dx[k]][v + dy[k]] == 3) {
              return;
            }
          }
        }
      }
  }
  for (int i = 0; i < (list1.size()); i++) {
    int u = list1[i].first;
    int v = list1[i].second;
    int cnt = 0;
    for (int k = 0; k < (4); k++)
      if (u + dx[k] > 0 && u + dx[k] <= m && v + dy[k] > 0 && v + dy[k] <= n) {
        int u2 = u + dx[k];
        int v2 = v + dy[k];
        if (a[u2][v2] == '1' && dd[u2][v2] == TEST) cnt++;
      }
    if (cnt != 2) return;
  }
  if (!list1.size()) return;
  TEST++;
  while (!q.empty()) q.pop();
  q.push(list1[0]);
  dd[list1[0].first][list1[1].second] = TEST;
  int cnt = 1;
  while (!q.empty()) {
    pair<int, int> tmp = q.front();
    q.pop();
    int u = tmp.first;
    int v = tmp.second;
    for (int k = 0; k < (4); k++)
      if (a[u + dx[k]][v + dy[k]] == '1' &&
          dd[u + dx[k]][v + dy[k]] == TEST - 1) {
        dd[u + dx[k]][v + dy[k]] = TEST;
        cnt++;
        q.push(make_pair(u + dx[k], v + dy[k]));
      }
  }
  if (cnt != list1.size()) return;
  res = max(res, (int)list1.size());
}
void refine(int x, int y) {
  while (!q.empty()) q.pop();
  q.push(make_pair(x, y));
  TEST++;
  dd[x][y] = TEST;
  while (!q.empty()) {
    pair<int, int> tmp = q.front();
    q.pop();
    int u = tmp.first;
    int v = tmp.second;
    for (int k = 0; k < (4); k++)
      if (u + dx[k] > 0 && u + dx[k] <= m && v + dy[k] > 0 && v + dy[k] <= n)
        if (a[u + dx[k]][v + dy[k]] == '0' && !dd[u + dx[k]][v + dy[k]]) {
          dd[u + dx[k]][v + dy[k]] = TEST;
          q.push(make_pair(u + dx[k], v + dy[k]));
        }
  }
}
int main() {
  scanf("%d%d", &m, &n);
  gets(s);
  for (int i = (1); i <= (m); i++) {
    gets(s);
    for (int j = (1); j <= (n); j++) a[i][j] = s[j - 1];
  }
  res = 0;
  for (int i = (1); i <= (m - 1); i++)
    for (int j = (1); j <= (n - 1); j++)
      if (a[i][j] == '1' && a[i][j + 1] == '1' && a[i + 1][j] == '1' &&
          a[i + 1][j + 1] == '1')
        res = 4;
  for (int i = (1); i <= (m); i++) {
    if (a[i][1] == '0' && !dd[i][1]) refine(i, 1);
    if (a[i][n] == '0' && !dd[i][n]) refine(i, n);
  }
  for (int j = (1); j <= (n); j++) {
    if (a[1][j] == '0' && !dd[1][j]) refine(1, j);
    if (a[m][j] == '0' && !dd[1][j]) refine(m, j);
  }
  for (int i = (1); i <= (m); i++)
    for (int j = (1); j <= (n); j++)
      if (a[i][j] == '0' && !dd[i][j]) {
        TEST++;
        bfs(i, j);
      }
  cout << res << endl;
  return 0;
}
