Flip and Turn

Time limit2sMemory limit64 MB

Problem

You are given a matrix $A$ of printable characters with $m$ rows and $n$ columns. Rows are numbered from $1$ by the first index and columns from $1$ by the second index. A single operation replaces the current matrix with a new matrix $B$ according to one of the following rules (the character in backticks is the operation identifier).

  • Transpose over the main diagonal (1): $B_{j,i} = A_{i,j}$
  • Transpose over the anti-diagonal (2): $B_{n-j+1,m-i+1} = A_{i,j}$
  • Horizontal flip (H): $B_{m-i+1,j} = A_{i,j}$
  • Vertical flip (V): $B_{i,n-j+1} = A_{i,j}$
  • Clockwise rotation by $90$ (A), $180$ (B), or $270$ (C) degrees; the $90$-degree case is $B_{j,m-i+1} = A_{i,j}$
  • Counterclockwise rotation by $90$ (X), $180$ (Y), or $270$ (Z) degrees; the $90$-degree case is $B_{n-j+1,i} = A_{i,j}$

You are given a sequence of at most 100,000 operations from this set. Apply them to the matrix in the given order and output the resulting matrix.

Input

The first line contains two integers $m$ and $n$ ($0 < m, n \le 300$). Each of the next $m$ lines contains exactly $n$ printable characters, where a printable character is a symbol whose ASCII code is between $33$ and $126$ inclusive; these lines contain no other symbols. The following line contains the sequence of operations, each given by its one-character identifier, to be applied from left to right.

Output

Print two integers: the number of rows and the number of columns of the resulting matrix. Then print the resulting matrix in the same format as the input.