var
f:array[0..101,0..101]of boolean;
st:array[0..101,0..101]of integer;
q:array[0..3,1..2]of integer;
i,j,ax,bx,ay,by,n,t,l:integer;
chr:char;
procedure still(a,b,j:integer);
begin
if (f[a+1,b]=true)and(st[a+1,b]>j)then begin st[a+1,b]:=j;still(a+1,b,j+1);end;
if (f[a-1,b]=true)and(st[a-1,b]>j)then begin st[a-1,b]:=j;still(a-1,b,j+1);end;
if (f[a,b+1]=true)and(st[a,b+1]>j)then begin st[a,b+1]:=j;still(a,b+1,j+1);end;
if (f[a,b-1]=true)and(st[a,b-1]>j)then begin st[a,b-1]:=j;still(a,b-1,j+1);end;
end;
function catch(i:longint):longint;
begin
if ((ax=bx)and(abs(ay-by)=1)) or ((ay=by)and(abs(ax-bx)=1))then exit(i);
if st[bx,by]=maxint then exit(-1)
else
begin
while st[bx,by]>i do
begin
inc(i);
while not(f[bx+q[l,1],by+q[l,2]]) do begin inc(l);l:=l mod 4;end;
bx:=bx+q[l,1];by:=by+q[l,2];
end;
end;
exit(i);
end;
begin
q[0,1]:=-1;q[0,2]:=0;
q[1,1]:=0;q[1,2]:=1;
q[2,1]:=1;q[2,2]:=0;
q[3,1]:=0;q[3,2]:=-1;
readln(n);
for i:=1 to n do
BEGIN
for j:=1 to n do
begin
read(chr);
if chr='F' then begin ax:=i;bx:=j;f[i,j]:=true;end;
if chr='J' then begin bx:=i;by:=j;f[i,j]:=true;end;
if chr='.' then f[i,j]:=true;
if chr='*' then f[i,j]:=false;
end;
readln;
end;
for i:=0 to n+1 do f[i,0]:=false;
for i:=0 to n+1 do f[i,n+1]:=false;
for j:=0 to n+1 do f[0,j]:=false;
for j:=0 to n+1 do f[n+1,j]:=false;
for i:=0 to n+1 do
for j:=0 to n+1 do st[i,j]:=maxint;
st[ax,bx]:=0;
still(ax,bx,0);
l:=0;
t:=catch(0);
if t=-1 then writeln('No solution')
else writeln(t);
end.
var
a:array[1..9]of integer;
i:integer;
begin
for i:=1 to 9 do read(a[i]);
i:=1;
while (a[i]=0)and(i<9) do inc(i);
if i=9 then write(a[i])
else
begin
if a[i]=1 then write('x^',9-i);
if a[i]=-1 then write('-x^',9-i);
if (a[i]<-1) or (a[i]>1) then write(a[i],'x^',9-i);
end;
inc(i);
while i<9 do
begin
while (a[i]=0)and(i<9) do inc(i);
if a[i]<-1 then write(' - ',0-a[i],'x^',9-i);
if a[i]=-1 then write(' - ','x^',9-i);
if a[i]=1 then write(' + ','x^',9-i);
if a[i]>1 then write(' + ',a[i],'x^',9-i);
inc(i);
end;
if a[9]<>0 then begin
if a[9]<0 then write(' - ',0-a[9]);
if a[9]>0 then write(' + ',a[9]);
end;
writeln;
end.