Shifting a Matrix

Parse a compressed shift-operation string with nested repetitions, apply the row and column rotations to an N by N matrix, and print the result.

Medium6SimulationImplementationArrayStringNo attempts yetTime limit5sMemory limit512 MB

Problem

You are given an N×NN \times N matrix AA whose entries start at Ai,j=(i1)N+jA_{i,j} = (i-1)N + j, where Ai,jA_{i,j} is the entry in row ii and column jj. Rows and columns are numbered from 1.

Four shift operations act on the matrix.

  • Left shift with ii rotates row ii one step to the left: the old Ai,kA_{i,k} becomes the new Ai,k1A_{i,k-1} for 2kN2 \le k \le N, and the old Ai,1A_{i,1} becomes the new Ai,NA_{i,N}.
  • Right shift with ii rotates row ii one step to the right: the old Ai,kA_{i,k} becomes the new Ai,k+1A_{i,k+1} for 1kN11 \le k \le N-1, and the old Ai,NA_{i,N} becomes the new Ai,1A_{i,1}.
  • Up shift with jj rotates column jj one step up: the old Ak,jA_{k,j} becomes the new Ak1,jA_{k-1,j} for 2kN2 \le k \le N, and the old A1,jA_{1,j} becomes the new AN,jA_{N,j}.
  • Down shift with jj rotates column jj one step down: the old Ak,jA_{k,j} becomes the new Ak+1,jA_{k+1,j} for 1kN11 \le k \le N-1, and the old AN,jA_{N,j} becomes the new A1,jA_{1,j}.

An operation sequence is given as one string and you apply its operations from left to right. The letters 'L', 'R', 'U', and 'D' name the left, right, up, and down shifts, and the number right after a letter is the row or column to shift, so "R25" is a right shift with 25. The notation also supports repetition. A sequence wrapped in parentheses and followed by a number mm repeats exactly mm times, so "(L1R2)10" repeats the pair of a left shift with 1 and a right shift with 2, in that order, 10 times.

Every operation sequence follows this grammar:

<sequence> := <sequence><repetition> | <sequence><operation> | <repetition> | <operation>
<repetition> := '('<sequence>')'<number>
<operation> := <shift><number>
<shift> := 'L' | 'R' | 'U' | 'D'
<number> := <nonzero_digit> | <number><digit>
<digit> := '0' | <nonzero_digit>
<nonzero_digit> := '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'

Given NN and an operation sequence, compute the N×NN \times N matrix left after the whole sequence.

Input

The input is a single test case in this format:

N L
S

The first line contains two integers NN and LL. NN (1N1001 \le N \le 100) is the size of the matrix and LL (2L10002 \le L \le 1000) is the length of the string on the second line. The second line contains the operation sequence SS, which follows the grammar above. Every row number and column number in SS is at least 1 and at most NN, and every repetition count is at least 1 and at most 10910^9.

Output

Print NN lines. The ii-th line holds the NN integers of row ii of AA after the operations, separated by single spaces.