#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ll long long
#define pii pair<int,int>
#define tii tuple <int,int,int>
#define N 200005
#define mod 1000000005
#define X first
#define Y second
#define eps 0.0000000001
#define all(x) x.begin(),x.end()
#define tot(x) x+1,x+n+1
using namespace std;
int sol, n, m, i,    j;
string s[550];
void up(int x, int y, int l) {
    sol = max(sol, l);

    if(x - 1 >= 0 && s[x - 1][y ] == '1')
        up(x - 1, y , l + 1);
}
void left(int x, int y, int l) {
    if(s[x ][y + 1] == '1')
        left(x , y + 1, l + 1);

    if(x - 1 >= 0 && s[x - 1][y ] == '1')
        up(x-1, y , l + 1);
}
void down(int x, int y, int l) {
    if(l == 1 && s[x + 1][y] != '1')
        return;

    if(s[x + 1][y] == '1')
        down(x + 1, y, l + 1);

    if(l != 1)
        if(s[x][y + 1] == '1')
            left(x, y + 1, l + 1);
}

int main() {
    cin.sync_with_stdio(0);
    cout.sync_with_stdio(0);
    cin >> n >> m;

    for(i = 0; i < n; i++) {
        cin >> s[i];
    }

    for(i = 0; i < n; i++)
        for(j = 0; j < m; j++)
            if(s[i][j] == '1')
                down(i, j, 1);

    if(sol == 1)
        sol = 0;

    cout << sol;
    return 0;
}