ICPC (International Connecting Points Company) starts to sell a new railway toy. It consists of a toy tramcar and many rail units on square frames of the same size. There are four types of rail units, namely, straight (S), curve (C), left-switch (L) and right-switch (R) as shown in Figure 9. A switch has three ends, namely, branch/merge-end (B/M-end), straight-end (S-end) and curve-end (C-end).

<image>

A switch is either in "through" or "branching" state. When the tramcar comes from B/M-end, and if the switch is in the through-state, the tramcar goes through to S-end and the state changes to branching; if the switch is in the branching-state, it branches toward C-end and the state changes to through. When the tramcar comes from S-end or C-end, it goes out from B/M-end regardless of the state. The state does not change in this case.

Kids are given rail units of various types that fill a rectangle area of w × h, as shown in Fig- ure 10(a). Rail units meeting at an edge of adjacent two frames are automatically connected. Each rail unit may be independently rotated around the center of its frame by multiples of 90 degrees in order to change the connection of rail units, but its position cannot be changed.

Kids should make "valid" layouts by rotating each rail unit, such as getting Figure 10(b) from Figure 10(a). A layout is valid when all rails at three ends of every switch are directly or indirectly connected to an end of another switch or itself. A layout in Figure 10(c) is invalid as well as Figure 10(a). Invalid layouts are frowned upon.

When a tramcar runs in a valid layout, it will eventually begin to repeat the same route forever. That is, we will eventually find the tramcar periodically comes to the same running condition, which is a triple of the tramcar position in the rectangle area, its direction, and the set of the states of all the switches.

<image>

A periodical route is a sequence of rail units on which the tramcar starts from a rail unit with a running condition and returns to the same rail unit with the same running condition for the first time. A periodical route through a switch or switches is called the "fun route", since kids like the rattling sound the tramcar makes when it passes through a switch. The tramcar takes the same unit time to go through a rail unit, not depending on the types of the unit or the tramcar directions. After the tramcar starts on a rail unit on a “fun route”, it will come back to the same unit with the same running condition, sooner or later. The fun time T of a fun route is the number of time units that the tramcar takes for going around the route.

Of course, kids better enjoy layouts with longer fun time. Given a variety of rail units placed on a rectangular area, your job is to rotate the given rail units appropriately and to find the fun route with the longest fun time in the valid layouts.

For example, there is a fun route in Figure 10(b). Its fun time is 24. Let the toy tramcar start from B/M-end at (1, 2) toward (1, 3) and the states of all the switches are the through-states. It goes through (1, 3), (1, 4), (1, 5), (2, 5), (2, 4), (1, 4), (1, 3), (1, 2), (1, 1), (2, 1), (2, 2) and (1, 2). Here, the tramcar goes through (1, 2) with the same position and the same direction, but with the different states of the switches. Then the tramcar goes through (1, 3), (1, 4), (2, 4), (2, 5), (1, 5), (1, 4), (1, 3), (1, 2), (2, 2), (2, 1), (1, 1) and (1, 2). Here, the tramcar goes through (1, 2) again, but with the same switch states as the initial ones. Counting the rail units the tramcar visited, the tramcar should have run 24 units of time after its start, and thus the fun time is 24.

There may be many valid layouts with the given rail units. For example, a valid layout containing a fun route with the fun time 120 is shown in Figure 11(a). Another valid layout containing a fun route with the fun time 148 derived from that in Figure 11(a) is shown in Figure 11(b). The four rail units whose rotations are changed from Figure 11(a) are indicated by the thick lines.

A valid layout depicted in Figure 12(a) contains two fun routes, where one consists of the rail units (1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (3, 2), (2, 2), (1, 2) with T = 8, and the other consists of all the remaining rail units with T = 18.

<image>

Another valid layout depicted in Figure 12(b) has two fun routes whose fun times are T = 12 and T = 20. The layout in Figure 12(a) is different from that in Figure 12(b) at the eight rail units rotated by multiples of 90 degrees. There are other valid layouts with some rotations of rail units but there is no fun route with the fun time longer than 20, so that the longest fun time for this example (Figure 12) is 20.

<image>

Note that there may be simple cyclic routes that do not go through any switches in a valid layout, which are not counted as the fun routes. In Figure 13, there are two fun routes and one simple cyclic route. Their fun times are 12 and 14, respectively. The required time for going around the simple cyclic route is 20 that is greater than fun times of the fun routes. However, the longest fun time is still 14.

<image>



Input

The input consists of multiple datasets, followed by a line containing two zeros separated by a space. Each dataset has the following format.

w h
a11 ... a1w
...
ah1 ... ahw


w is the number of the rail units in a row, and h is the number of those in a column. aij (1 ≤ i ≤ h, 1 ≤ j ≤ w) is one of uppercase letters 'S', 'C', 'L' and 'R', which indicate the types of the rail unit at (i, j) position, i.e., straight, curve, left-switch and right-switch, respectively. Items in a line are separated by a space. You can assume that 2 ≤ w ≤ 6, 2 ≤ h ≤ 6 and the sum of the numbers of left-switches and right-switches is greater than or equal to 2 and less than or equal to 6.

Output

For each dataset, an integer should be printed that indicates the longest fun time of all the fun routes in all the valid layouts with the given rail units. When there is no valid layout according to the given rail units, a zero should be printed.

Example

Input

5 2
C L S R C
C C S C C
6 4
C C C C C C
S L R R C S
S S S L C S
C C C C C C
6 6
C L S S S C
C C C S S C
C C C S S C
C L C S S C
C C L S S C
C S L S S C
6 6
C S S S S C
S C S L C S
S C S R C S
S C L S C S
S C R S C S
C S S S S C
4 4
S C C S
S C L S
S L C S
C C C C
6 4
C R S S L C
C R L R L C
C S C C S C
C S S S S C
0 0


Output

24
20
148
14
0
178
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
char in[2];
char t[8][8];
int conv[8][8];
int H,W;
int ret;
int used[20][20];
int pr[8];
int pc[8];
int pd[8];
int lr[4][3]={{1,1,0},{0,2,1},{1,1,2},{2,0,1}};
int lc[4][3]={{0,2,1},{1,1,2},{2,0,1},{1,1,0}};
int rr[4][3]={{1,1,2},{0,2,1},{1,1,0},{2,0,1}};
int rc[4][3]={{0,2,1},{1,1,0},{2,0,1},{1,1,2}};
int at[8][3];
int au[8][3];
int al[8][3];
int ps;
int v[8][3][1<<6];
void go(int a,int b){
	for(int i=0;i<ps;i++)for(int j=0;j<3;j++)for(int k=0;k<(1<<ps);k++)v[i][j][k]=-1;
	int len=0;
	int A=a;
	int B=b;
	int bit=0;
	while(1){
		v[A][B][bit]=len;
		len+=al[A][B]+1;
		int ta=at[A][B];int tb=au[A][B];
		A=ta;
		if(tb==0){
			if(bit&(1<<A))B=1;
			else B=2;
			bit^=(1<<A);
		}else B=0;
		if(~v[A][B][bit])break;
	}
	ret=max(ret,len-v[A][B][bit]);
}
void calc(){
	/*for(int i=0;i<ps;i++){
		for(int j=0;j<3;j++){
			printf("(%d, %d, %d)",at[i][j],au[i][j],al[i][j]);
		}
		printf("\n");
	}
	printf("\n");*/
	for(int i=0;i<ps;i++)for(int j=0;j<3;j++){
		go(i,j);
	}
}
void dfs_route(int P1,int P2,int TR,int TC,int MR,int MC,int L){
	if(MR<0||MC<0||MR>=H||MC>=W)return;
	if(~conv[MR][MC]){
		int M=conv[MR][MC];
		bool ok=false;
		for(int i=0;i<3;i++){
			if(!~at[M][i]&&((t[MR][MC]=='L'&&TR==MR*2+lr[pd[M]][i]&&TC==MC*2+lc[pd[M]][i])||(t[MR][MC]=='R'&&TR==MR*2+rr[pd[M]][i]&&TC==MC*2+rc[pd[M]][i]))){
				at[P1][P2]=M;au[P1][P2]=i;
				at[M][i]=P1;au[M][i]=P2;
				al[P1][P2]=al[M][i]=L;
				ok=true;break;
			}
		}
		if(!ok)return;
		for(int i=0;i<ps;i++){
			for(int j=0;j<3;j++){
				if(!~at[i][j]){
					int sr=pr[i]*2;
					int sc=pc[i]*2;
					int kr=pr[i];int kc=pc[i];
					if(t[pr[i]][pc[i]]=='L'){sr+=lr[pd[i]][j];sc+=lc[pd[i]][j];
					if(lr[pd[i]][j]==0)kr--;if(lr[pd[i]][j]==2)kr++;
					if(lc[pd[i]][j]==0)kc--;if(lc[pd[i]][j]==2)kc++;}
					if(t[pr[i]][pc[i]]=='R'){sr+=rr[pd[i]][j];sc+=rc[pd[i]][j];
					if(rr[pd[i]][j]==0)kr--;if(rr[pd[i]][j]==2)kr++;
					if(rc[pd[i]][j]==0)kc--;if(rc[pd[i]][j]==2)kc++;}
					dfs_route(i,j,sr,sc,kr,kc,0);
					at[at[P1][P2]][au[P1][P2]]=au[at[P1][P2]][au[P1][P2]]=-1;
					at[P1][P2]=au[P1][P2]=-1;
					return;
				}
			}
		}
		calc();
		at[at[P1][P2]][au[P1][P2]]=au[at[P1][P2]][au[P1][P2]]=-1;
		at[P1][P2]=au[P1][P2]=-1;
		return;
	}
	if(used[MR][MC])return;
	used[MR][MC]=1;
	if(t[MR][MC]=='S'){
		int nr,nc,mr,mc;
		if(TR%2){
			nr=TR;nc=MC*4+2-TC;
			mr=MR;mc=(nc-MC-1);
		}else{
			nc=TC;nr=MR*4+2-TR;
			mc=MC;mr=(nr-MR-1);
		}
		dfs_route(P1,P2,nr,nc,mr,mc,L+1);
	}else{
		if(TR%2){
			dfs_route(P1,P2,MR*2,MC*2+1,MR-1,MC,L+1);
			dfs_route(P1,P2,MR*2+2,MC*2+1,MR+1,MC,L+1);
		}else{
			dfs_route(P1,P2,MR*2+1,MC*2,MR,MC-1,L+1);
			dfs_route(P1,P2,MR*2+1,MC*2+2,MR,MC+1,L+1);
		}
	}
	used[MR][MC]=0;
}
void dfs_dir(int a){
	if(a==ps){
		for(int i=0;i<ps;i++)for(int j=0;j<3;j++){
			at[i][j]=au[i][j]=-1;
		}
		for(int i=0;i<H;i++)for(int j=0;j<W;j++)used[i][j]=0;
		int sr=pr[0]*2;
		int sc=pc[0]*2;
		int kr=pr[0];int kc=pc[0];
		if(t[pr[0]][pc[0]]=='L'){sr+=lr[pd[0]][0];sc+=lc[pd[0]][0];
		if(lr[pd[0]][0]==0)kr--;if(lr[pd[0]][0]==2)kr++;
		if(lc[pd[0]][0]==0)kc--;if(lc[pd[0]][0]==2)kc++;}
		if(t[pr[0]][pc[0]]=='R'){sr+=rr[pd[0]][0];sc+=rc[pd[0]][0];
		if(rr[pd[0]][0]==0)kr--;if(rr[pd[0]][0]==2)kr++;
		if(rc[pd[0]][0]==0)kc--;if(rc[pd[0]][0]==2)kc++;}
		dfs_route(0,0,sr,sc,kr,kc,0);
		return;
	}
	for(int i=0;i<4;i++){
		pd[a]=i;
		dfs_dir(a+1);
	}
}
int main(){
	int a,b;
	while(scanf("%d%d",&b,&a),a){
		H=a;W=b;
		for(int i=0;i<a;i++)for(int j=0;j<b;j++){
			scanf("%s",in);
			t[i][j]=in[0];
		}
		for(int i=0;i<a;i++)for(int j=0;j<b;j++)conv[i][j]=-1;
		ret=0;
		ps=0;
		for(int i=0;i<a;i++)for(int j=0;j<b;j++){
			if(t[i][j]=='L'||t[i][j]=='R'){
				conv[i][j]=ps;
				pr[ps]=i;pc[ps++]=j;
			}
		}
		if(ps%2){
			printf("0\n");continue;
		}
		dfs_dir(0);
		printf("%d\n",ret);
	}
}