F. Arrow Game
time limit per test
10 seconds
memory limit per test
192 megabytes
input
standard input
output
standard output

You are given an initial configuration of $$$N$$$ cells (from left to right), where each cell can have one of the 4 things: empty, tombstone, left arrow or right arrow.

Two players play a game on it, starting from Player A followed by Player B turn-by-turn.

  • Player A: picks a currently empty position (say index $$$z$$$) OR ends the game.
  • Player B: replaces every right arrow on the left of $$$z$$$ with a tombstone AND replaces every left arrow on the right of $$$z$$$ with a tombstone (i.e. replace all arrows pointing towards z with a tombstone) AND then assigns an arrow to position $$$z$$$. Player B chooses whether to assign a left arrow or right arrow.

Player A's objective is to maximize the number of arrows (which were not turned to tombstones) when the game ends and Player B's objective is to minimize the number of arrows when the game ends.

Find the number of arrows at the end of the game if both players play optimally.

Input
  • The first line contains $$$T (1 \le T \le 10^5)$$$ and $$$ N (1 \le N \le 20)$$$, the number of test cases and the sizes of all the input strings.
  • The $$$i^{\text{th}}$$$ of the next $$$T$$$ lines each contain string $$$S$$$ of size $$$N$$$, representing the initial configuration of cells.
  • In the input string $$$S$$$, X represents an empty spot, T represents a tombstone, L represents a left arrow, R represents a right arrow.
Output

For each test case, output the number of arrows at the end of game under optimal play.

Examples
Input
4 4
XXXX
LTXR
RTXL
LRTR
Output
2
3
2
3
Input
4 4
XXXX
LTXR
RTXL
LRTR
Output
2
3
2
3
Note
  • Initially, all the cells are empty:
  • Player A chooses $$$z = 2$$$
  • Player B chooses to assign a right arrow to $$$z = 2$$$. The configuration now is XRXX:

  • Player A chooses $$$z = 3$$$
  • Player B replaces the right arrow at $$$z = 2$$$ with a tombstone and chooses to assign a right arrow to $$$z = 3$$$. The configuration now is XTRX:

  • Player A chooses $$$z = 1$$$
  • Player B chooses to assign a left arrow to $$$z = 1$$$. The configuration now is LTRX:

  • Player A ends the game with $$$2$$$ arrows.