uses math;
var
a,b,c,d,q1,q2,ans,ans1,i:longint;
v:array[1..100000,1..4]of longint;
r:array[-1..300,-1..100]of char;
p:array[-1..300,-1..100]of boolean;
s:array[1..300]of string;
const t:array[1..4,1..2]of longint=((-1,0),(0,-1),(1,0),(0,1));
begin
readln(a,b);
a:=2*a+1;
b:=2*b+1;
for c:=1 to b do
begin
readln(s[c]);
for d:=1 to a do
begin
r[c,d]:=s[c][d];
end;
end;
fillchar(p,sizeof(p),true);
for c:=1 to a do
begin
if (r[1,c]<>'-')and(r[1,c]<>'+')and(r[1,c]<>'|') then
begin
inc(q2);
v[q2,1]:=2;
v[q2,2]:=c;
v[q2,3]:=1;
v[q2,4]:=q2;
p[2,c]:=false;
end;
if (r[b,c]<>'-')and(r[b,c]<>'+')and(r[b,c]<>'|') then
begin
inc(q2);
v[q2,1]:=b-1;
v[q2,2]:=c;
v[q2,3]:=1;
v[q2,4]:=q2;
p[b-1,c]:=false;
end;
end;
for c:=1 to b do
begin
if (r[c,1]<>'-')and(r[c,1]<>'+')and(r[c,1]<>'|') then
begin
inc(q2);
v[q2,1]:=c;
v[q2,2]:=2;
v[q2,3]:=1;
v[q2,4]:=q2;
p[c,2]:=false;
end;
if (r[c,a]<>'-')and(r[c,a]<>'+')and(r[c,a]<>'|') then
begin
inc(q2);
v[q2,1]:=c;
v[q2,2]:=a-1;
v[q2,3]:=1;
v[q2,4]:=q2;
p[c,a-1]:=false;
end;
end;
while q1<q2 do
begin
inc(q1);
for i:=1 to 4 do
begin
if (r[(v[q1,1]+t[i,1]),(v[q1,2]+t[i,2])]<>'-')and
(r[(v[q1,1]+t[i,1]),(v[q1,2]+t[i,2])]<>'|')and
(r[(v[q1,1]+t[i,1]+t[i,1]),(v[q1,2]+t[i,2]+t[i,2])]<>'-')and
(r[(v[q1,1]+t[i,1]+t[i,1]),(v[q1,2]+t[i,2]+t[i,2])]<>'|')and
(p[(v[q1,1]+t[i,1]+t[i,1]),(v[q1,2]+t[i,2]+t[i,2])]=true)and
((v[q1,1]+t[i,1]+t[i,1])in[1..b])and
((v[q1,2]+t[i,2]+t[i,2])in[1..a]) then
begin
inc(q2);
v[q2,1]:=v[q1,1]+t[i,1]+t[i,1];
v[q2,2]:=v[q1,2]+t[i,2]+t[i,2];
v[q2,3]:=v[q1,3]+1;
v[q2,4]:=v[q1,4];
p[v[q2,1],v[q2,2]]:=false;
if v[q2,4]=1 then ans:=max(ans,v[q2,3]);
if v[q2,4]=2 then ans1:=max(ans1,v[q2,3]);
end;
end;
end;
writeln(max(1,max(ans,ans1)));
end.