Dice on a Board

No attempts yetTime limit1sMemory limit128 MB

Problem

You and your friends got bored of chess and backgammon, so you made a new game. It is a single player game played with one backgammon die (the singular of dice) on a board that looks like a chess board.

The board has NN rows and MM columns. Every cell is either empty or holds one digit from 00 to 99. A die with the numbers 11 to 66 on its six faces sits on the starting cell, and the edges of its bottom face are aligned with the axes of the board. Your goal is to move it to the target cell.

Rows are numbered 11 to NN from top to bottom, and columns 11 to MM from left to right. Forward is the direction of decreasing row number, backward the direction of increasing row number, right the direction of increasing column number, and left the direction of decreasing column number.

The initial orientation of the die is given by a string SS, a permutation of the digits 11 to 66. Its characters give the number on the right, left, forward, backward, top and bottom face, in that order.

The die moves under these rules.

  1. You move the die to one of its four adjacent cells by flipping it onto the face pointing that way. For example, if the current orientation is 136425 and you move the die to the cell on its right, the right face becomes the bottom face in the new cell, so the orientation becomes 256431.
  2. Your score starts at 00. When you move the die, if the number on the new bottom face equals the number in the cell you just entered, your score increases by the sum of those two numbers, otherwise it decreases by the sum of those two numbers. Entering the target cell does not change your score.
  3. You can not leave the board.
  4. Once you leave the starting cell, you can not enter it again.
  5. Once you enter the target cell, you can not leave it.
  6. You can not enter a cell that holds no number. The target cell is the only exception.

Given the board, the starting cell, the target cell and the initial orientation of the die, find the maximum score you can end up with after moving the die from the starting cell to the target cell under these rules.

Input

The first line contains a single integer TT, the number of test cases (1T2001 \le T \le 200). The specifications of TT test cases follow.

Each test case is given in N+2N + 2 lines. The first line contains two integers NN and MM (1N,M101 \le N, M \le 10), the number of rows and the number of columns of the board. The second line contains the string SS describing the initial orientation of the die on the starting cell. Each of the remaining NN lines contains MM characters, where the jj-th character of the ii-th line is the value of the cell in row ii, column jj. Each character is one of the following.

  1. . is an empty cell.
  2. S is the starting cell, which appears exactly once on the board.
  3. T is the target cell, which appears exactly once on the board.
  4. A digit from 0 to 9 is the value written in that cell.

Output

For each test case, print one line containing one of these.

  1. Impossible if you can not reach the target cell from the starting cell.
  2. Infinity if your final score has no upper limit and you can raise it without end.
  3. Otherwise, the maximum score you can get.