406767: GYM102536 F One Great Grater
Description
You've spent the past couple of years as a Chugent, fulfilling various types of missions. You've infiltrated the Pentacontagon. You prevented the bombing that attempted to kill the not-so-rich King P'Taka. Despite doubts, you were also able to apprehend the master thief Pin Lu V. Amidst all these, you kept up with your daily regimen: 100 push-ups, 100 sit-ups, 100 squats, and a 10 km run. You kept at it every single day. You feel healthier than ever, but you think that your age is catching up to you as you notice your hair thinning for some reason.
With these thoughts in mind, you go to your mentor, the Great Spaitama. You tell him that it's time to be promoted from a Chugent to a full-fledged Jogent. He agrees, and presents you with a final test. He takes off and runs a few kilometers worth, but you are able to quickly follow him due to your daily training. You end up at the roof of what looks like a giant warehouse. You find and examine a secret door, and realize that it uses locks that are among the hardest to break into in the world. Thankfully, you are with no other than the Great Spaitama, who can open any lock with just ONE kick! It's not your first time seeing his technique, but you are still amazed at how he can kick the lock open without making any sound.
With the door open, you can now see the big rectangular room below you, and attached on one of the walls, you see something shiny.
"Is that a cheese grater?", you ask.
The Great Spaitama replies, "Not quite. You use it for fruits, and is worth a couple of thousands. A small, soft-spoken, client approached us to steal it."
He continues, "Note the floor tiles below. There are blue, red, and white tiles. Upon stepping on a blue tile, you end up turning and moving to your right. Stepping on a red tile sends you to your left. White tiles cause you to move forward."
He then hands you a pair of gloves. "Use these to stick onto the wall. Unfortunately, I can't drop you to the wall directly, and can only drop you on ONE specific tile. You can choose the direction (north, south, west, east) that you will be facing though. You just need to end up on a tile that will push you towards the wall. Let's call the border of a tile adjacent to the wall a "wall segment". You just need to end up at any of those wall segments; getting to the grater will then be easy with those gloves."
The following illustrates the "wall segments". There are five wall segments at the top and bottom walls each, and three wall segments at the left and right walls each.
"ONE last thing. Should you need it, I can help you change a white tile to either blue or red. Only ONE though, or it would be too easy for you. Understood?"
You tell The Great Spaitama that you are ready to begin. You still don't get his affinity with the number ONE. In any case, you try to impress him by not only getting to ONE wall segment, but telling him that you can actually get to other wall segments as well.
How many distinct wall segments can you possibly reach, if you can change at most one white tile to either blue or red?
InputThe first line of input contains a single integer $$$t$$$ denoting the number of test cases.
The first line of each test case contains two space-separated integers $$$h$$$ and $$$w$$$ denoting the height and width of the grid, respectively.
The next $$$h$$$ lines each contains a string of length $$$w$$$ denoting a row of the grid (from top to bottom). Each string consists of the following characters:
- W denoting a white tile;
- R denoting a red tile;
- B denoting a blue tile;
- S denoting the tile where you are dropped into. This tile is white.
Constraints
- $$$1 \le t \le 10^4$$$
- $$$1 \le h, w \le 10^5$$$
- The sum of the $$$hw$$$ across all test cases is $$$\le 4\cdot 10^5$$$
- There is exactly one S in the grid.
For each test case, first output a single line containing a single integer $$$d$$$ denoting the number of distinct wall segments you can reach. Then, print $$$d$$$ lines describing these wall segments. Each of these lines contains a character $$$x$$$, a space, and then an integer $$$k$$$.
- If the wall segment is at the top edge, then $$$x$$$ is the letter T, and $$$k$$$ denotes the column number from $$$1$$$ to $$$w$$$, from left to right.
- If the wall segment is at the bottom edge, then $$$x$$$ is the letter B, and $$$k$$$ denotes the column number from $$$1$$$ to $$$w$$$, from left to right.
- If the wall segment is at the left edge, then $$$x$$$ is the letter L, and $$$k$$$ denotes the row number from $$$1$$$ to $$$h$$$, from top to bottom.
- If the wall segment is at the right edge, then $$$x$$$ is the letter R, and $$$k$$$ denotes the row number from $$$1$$$ to $$$h$$$, from top to bottom.
Output the wall segments in sorted order, first by increasing $$$x$$$ (alphabetically) and then by increasing $$$k$$$.
ExampleInput2 3 5 RBWWW WWWWW SWWBW 5 1 W W R W SOutput
10 B 1 B 2 B 3 B 4 L 1 L 2 L 3 R 1 R 2 T 3 6 B 1 L 3 L 4 L 5 R 4 R 5Note
The following illustrates the sample input:
The gray wall segments cannot be reached. The remaining wall segments can be reached.