import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main2 { static int h,w; static char list[][]; static int dp[][][]; static int goalX, goalY; static final int NIGHT = 0; static final int BISHOP = 1; static int nDirX[] = { 1, 1, -1, -1, 2, 2, -2, -2 }; static int nDirY[] = { 2, -2, 2, -2, 1, -1, 1, -1 }; static int bDirX[] = { 1, 1, -1, -1 }; static int bDirY[] = { 1, -1, 1, -1 }; public static void main(String[] args) { try (Scanner scan = new Scanner(System.in)) { h = Integer.parseInt(scan.next()); w = Integer.parseInt(scan.next()); list = new char[h][w]; dp = new int[h][w][2]; int startX = 0, startY = 0; for(int i=0; i q = new LinkedList<>(); Node s = new Node(startX, startY, 0, NIGHT); q.add(s); while(!q.isEmpty()) { Node now = q.poll(); if(now.x == goalX && now.y == goalY) { System.out.println(now.steps); System.exit(0); } if(now.state == NIGHT) { if(dp[now.y][now.x][NIGHT] > now.steps) { dp[now.y][now.x][NIGHT] = now.steps; } else { continue; } } else { if(dp[now.y][now.x][BISHOP] > now.steps) { dp[now.y][now.x][BISHOP] = now.steps; } else { continue; } } if(now.state == NIGHT) { for(int i=0; i<8; i++) { int nextX = now.x + nDirX[i]; int nextY = now.y + nDirY[i]; if(nextX >= 0 && nextX <= w-1 && nextY >= 0 && nextY <= h-1 ) { int nextState = now.state; if(list[nextY][nextX] == 'R') { nextState = BISHOP; } Node next = new Node(nextX, nextY, now.steps+1, nextState); q.add(next); } } } else if(now.state == BISHOP) { for(int i=0; i<4; i++) { int nextX = now.x + bDirX[i]; int nextY = now.y + bDirY[i]; if(nextX >= 0 && nextX <= w-1 && nextY >= 0 && nextY <= h-1 ) { int nextState = now.state; if(list[nextY][nextX] == 'R') { nextState = NIGHT; } Node next = new Node(nextX, nextY, now.steps+1, nextState); q.add(next); } } } } } } class Node { int x, y; int steps; int state; Node(int x, int y, int steps, int state) { this.x = x; this.y = y; this.steps = steps;; this.state = state; } }