#include <bits/stdc++.h>
using namespace std;
const int u[8][2] = {{0, 1}, {0, -1}, {1, 0},  {-1, 0},
                     {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
char field[1000][1001];
bool v[1000][1000], check[1000][1000];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", field[i]);
  int ans = 0;
  for (int i = 0; i < n - 1; i++)
    for (int j = 0; j < m - 1; j++)
      if (field[i][j] == '1' && field[i][j + 1] == '1')
        if (field[i + 1][j] == '1' && field[i + 1][j + 1] == '1') ans = 4;
  memset(v, false, sizeof(v));
  memset(check, false, sizeof(check));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (!v[i][j] && field[i][j] == '0') {
        vector<pair<int, int> > L;
        queue<pair<int, int> > Q;
        Q.push(make_pair(i, j));
        v[i][j] = true;
        bool flag = true;
        if (i == 0 || i == n - 1 || j == 0 || j == m - 1) flag = false;
        while (!Q.empty()) {
          pair<int, int> cur = Q.front();
          Q.pop();
          for (int k = 0; k < 8; k++) {
            int t1 = cur.first + u[k][0];
            int t2 = cur.second + u[k][1];
            if (0 <= t1 && t1 < n && 0 <= t2 && t2 < m)
              if (field[t1][t2] == '0' && !v[t1][t2]) {
                v[t1][t2] = true;
                Q.push(make_pair(t1, t2));
                if (t1 == 0 || t1 == n - 1 || t2 == 0 || t2 == m - 1)
                  flag = false;
              } else if (field[t1][t2] == '1' && !check[t1][t2]) {
                L.push_back(make_pair(t1, t2));
                check[t1][t2] = true;
              }
          }
        }
        if (L.size() == 0 || !flag) {
          for (int k = 0; k < L.size(); k++)
            check[L[k].first][L[k].second] = false;
          continue;
        }
        Q.push(L[0]);
        v[L[0].first][L[0].second] = true;
        int cnt = 1;
        while (!Q.empty()) {
          pair<int, int> cur = Q.front();
          Q.pop();
          int tmp = 0;
          for (int k = 0; k < 4; k++) {
            int t1 = cur.first + u[k][0];
            int t2 = cur.second + u[k][1];
            if (0 <= t1 && t1 < n && 0 <= t2 && t2 < m) {
              if (check[t1][t2] && !v[t1][t2]) {
                v[t1][t2] = true;
                Q.push(make_pair(t1, t2));
                cnt++;
              }
              if (check[t1][t2]) tmp++;
            }
          }
          if (tmp != 2) flag = false;
        }
        if (flag && cnt == L.size()) ans = max(ans, cnt);
        for (int k = 0; k < L.size(); k++) {
          check[L[k].first][L[k].second] = false;
          v[L[k].first][L[k].second] = false;
        }
      }
  printf("%d\n", ans);
  return 0;
}
