#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int map[110][110],n,m,visited[110][110],M;
int x[]={1,0,0},y[]={0,-1,1};
typedef struct
{
int x,y;
}Node;
queue<Node>q;
Node node;
void bfs()
{
int x1,y1,x2,y2;
while(!q.empty())
{
x1=q.front().x;
y1=q.front().y;
if(x1>M)
M=x1;
q.pop();
for(int k=0;k<3;k++)
{
x2=x1+x[k];y2=y1+y[k];
if(x2>=1&&x2<=n&&y2>=1&&y2<=m&&map[x2][y2]&&!visited[x2][y2])
{
node.x=x2;node.y=y2;
if(x2>M) M=x2;
visited[x2][y2]=1;
q.push(node);
}
}
}
}
int main()
{
int i,j,k;
while(cin>>n>>m)
{
for(i=n;i>=1;i--)
for(j=m;j>=1;j--)
cin>>map[i][j];
memset(visited,0,sizeof(visited));
M=0;
for(i=1;i<=m;i++)
{
if(map[1][i])
{
node.x=1;
node.y=i;
q.push(node);
M=1;
}
}
bfs();
cout<<M<<endl;
}
return 0;
}