var
a,b:array[1..10,1..10] of integer;

i,j,k,n,m,x,y,bigloop,max:longint;
c,c2:char;
s:string;

procedure fill(x,y:longint);
var
i,j:longint;
begin
	if (b[x+1,y]>b[x,y]+1) and (x<10) then begin
		b[x+1,y]:=b[x,y]+1;
		fill(x+1,y)
		end;
		
	if (b[x-1,y]>b[x,y]+1) and (x>1) then begin
		b[x-1,y]:=b[x,y]+1;
		fill(x-1,y)
		end;
	if (b[x,y+1]>b[x,y]+1) and (y<10) then begin
		b[x,y+1]:=b[x,y]+1;
		fill(x,y+1)
		end;
	if (b[x,y-1]>b[x,y]+1) and (y>1) then begin
		b[x,y-1]:=b[x,y]+1;
		fill(x,y-1)
		end;
	end;
begin
assign(input,'DATA4.txt'); reset(input);
assign(output,'OUT4.txt'); rewrite(output);
for bigloop:=1 to 5 do begin
	for i:=1 to 10 do begin
		for j:=1 to 10 do begin
			read(c);
			if c='.' then a[i,j]:=-1;
			if c='T' then a[i,j]:=999;
			if c='F' then a[i,j]:=0;
				
			b[i,j]:=a[i,j];
			end;
		readln
		end;
	readln(s);
	for i:=1 to 10 do
		for j:=1 to 10 do
			if b[i,j]=0 then fill(i,j);
	max:=-1;
	for i:=1 to 10 do 
		for j:=1 to 10 do
			if b[i,j]>max then max:=b[i,j];
	
	if max=999 then writeln('-1') else writeln(max)
	end;
close(input);
close(output);
end.
