#include <bits/stdc++.h>
using namespace std;
const int N = 1123;
char a[N][N];
bool used[N][N];
int dx[] = {1, 0, 0, -1, 1, 1, -1, -1};
int dy[] = {0, -1, 1, 0, 1, -1, 1, -1};
int n, m;
set<pair<int, int> > st;
bool ok = 1;
void dfs(int x, int y) {
  used[x][y] = 1;
  if (x == 1 || y == 1 || x == n || y == m) ok = 0;
  for (int i = 0; i < 8; i++) {
    int xx = x + dx[i];
    int yy = y + dy[i];
    if (xx >= 1 && xx <= n && yy >= 1 && yy <= m) {
      if (used[xx][yy]) continue;
      if (a[xx][yy] == '1') {
        st.insert(make_pair(xx, yy));
      } else {
        dfs(xx, yy);
      }
    }
  }
}
int cm[N][N];
void dfs1(int x, int y, int c) {
  cm[x][y] = c;
  for (int i = 0; i < 4; i++) {
    int xx = x + dx[i];
    int yy = y + dy[i];
    if (xx >= 1 && xx <= n && yy >= 1 && yy <= m && cm[xx][yy] == 0 &&
        a[xx][yy] == '1') {
      dfs1(xx, yy, c);
    }
  }
}
int main() {
  int i, j;
  cin >> n >> m;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) cin >> a[i][j];
  int ans = 0;
  int t = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (cm[i][j] == 0 && a[i][j] == '1') dfs1(i, j, ++t);
    }
  }
  int x, y;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (!used[i][j] && a[i][j] == '0') {
        st.clear();
        ok = 1;
        dfs(i, j);
        if (ok) {
          int w = 0;
          for (set<pair<int, int> >::iterator it = st.begin(); it != st.end();
               it++) {
            x = (*it).first;
            y = (*it).second;
            if (cm[x][y] != w) {
              if (w != 0) {
                ok = 0;
                break;
              } else
                w = cm[(*it).first][(*it).second];
            }
            int cnt = 0;
            for (int l = 0; l < 4; l++) {
              int xx = x + dx[l];
              int yy = y + dy[l];
              if (xx >= 1 && xx <= n && yy >= 1 && yy <= m &&
                  st.count(make_pair(xx, yy))) {
                cnt++;
              }
            }
            if (cnt != 2) ok = 0;
          }
          if (ok && ans < st.size()) ans = st.size();
        }
      }
    }
  }
  int ss = 0;
  for (i = 1; i < n; i++) {
    int w = 0;
    for (j = 1; j <= m; j++) {
      if (a[i][j] == '1' && a[i + 1][j] == '1')
        w++;
      else
        w = 0;
      ss = max(ss, w * 2);
    }
  }
  for (i = 1; i < m; i++) {
    int w = 0;
    for (j = 1; j <= n; j++) {
      if (a[j][i] == '1' && a[j][i + 1] == '1')
        w++;
      else
        w = 0;
      ss = max(ss, w * 2);
    }
  }
  if (ss < 4) ss = 0;
  if (ss > 4) ss = 4;
  ans = max(ss, ans);
  cout << ans;
}
