#include #include #include #include #include #include using namespace std; int dp[2][500][500]; const int inf=1e9; int nx[]={-2,-2,-1,-1,1,1,2,2}; int ny[]={1,-1,2,-2,2,-2,1,-1}; int bx[]={-1,-1,1,1}; int by[]={1,-1,1,-1}; int h,w; bool isInField(int x,int y){ if(x<0 or w<=x) return false; if(y<0 or h<=y) return false; return true; } int main(){ cin>>h>>w; vector s(h); int sx=0,sy=0; int gx=0,gy=0; for(int i=0;i>s[i]; for(int j=0;j > q; dp[0][sy][sx]=0; q.push(make_tuple(0,sx,sy)); while(!q.empty()){ auto it=q.front(); q.pop(); int mode=get<0>(it); int x=get<1>(it); int y=get<2>(it); if(x==gx and y==gy){ cout<dp[mode][y][x]+1){ dp[nextmode][dy][dx]=dp[mode][y][x]+1; q.push(make_tuple(nextmode,dx,dy)); } } } else{ for(int i=0;i<4;i++){ int dx=x+bx[i]; int dy=y+by[i]; int nextmode=mode; if(!isInField(dx,dy)) continue; if(s[dy][dx]=='R'){ nextmode^=1; } if(dp[nextmode][dy][dx]>dp[mode][y][x]+1){ dp[nextmode][dy][dx]=dp[mode][y][x]+1; q.push(make_tuple(nextmode,dx,dy)); } } } } cout<<-1<