var
i,j,k,n,m,x,s,t:longint;
a,f:array [0..1000,0..1000] of longint;
function max(x,y:longint):longint;
begin
if x>y then max:=x
else max:=y;
end;
begin
read(n,m);
for i:=1 to n do
for j:=1 to m do
begin
read(a[i,j]);
if a[i,j]=1 then f[i,j]:=1;
end;
for i:=1 to n do
begin

for j:=1 to m do
if (a[i-1,j-1]=1) and (a[i,j]=1) then
begin

t:=0;
for k:=j-f[i-1,j-1] to j-1 do
if a[i,k]=0 then t:=1;
for k:=i-f[i-1,j-1] to i-1 do
if a[k,j]=0 then t:=1;
if t=0 then f[i,j]:=max(f[i,j],f[i-1,j-1]+1);
end;
end;
s:=0;
for i:=1 to n do
for j:=1 to m do
if f[i,j]>s then s:=f[i,j];
write(s);
end.