CF BUDDY
← Problems·

1607F · Robot on the Board 2

2300 · brute force, dfs and similar, graphs

Problem: The robot is located on a checkered rectangular board of size n×mn \times m (nn rows, mm columns). The rows in the board are numbered from 11 to nn from top to bottom, and the columns — from 11 to mm from left to right.

The robot is able to move from the current cell to one of the four cells adjacent by side.

Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.

The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move.

  • If the robot moves beyond the edge of the board, it falls and breaks.
  • If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore).

Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.

Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board).

Input Format: The first line contains an integer tt (1t100001 \le t \le 10000) — the number of test cases in the test.

Each test case's description is preceded by a blank line. Next is a line that contains integers nn and mm (1n20001 \le n \le 2000; 1m20001 \le m \le 2000) — the height and width of the board. This line followed by nn lines, the ii-th of which describes the ii-th line of the board. Each of them is exactly mm letters long and consists of symbols 'L', 'R', 'D' and 'U'.

It is guaranteed that the sum of sizes of all boards in the input does not exceed 41064\cdot10^6.

Output Format: For each test case, output three integers rr, cc and dd (1rn1 \le r \le n; 1cm1 \le c \le m; d0d \ge 0), which denote that the robot should start moving from cell (r,c)(r, c) to make the maximum number of moves dd. If there are several answers, output any of them.

Sample Cases

Case 1

Input

7

1 1
R

1 3
RRL

2 2
DL
RU

2 2
UD
RU

3 2
DL
UL
RU

4 4
RRRD
RUUD
URUD
ULLR

4 4
DDLU
RDDU
UUUU
RDLD

Output

1 1 1
1 1 3
1 1 4
2 1 3
3 1 5
4 3 12
1 1 4

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback