Example

Input

3 2
R1


Output

3 1 2
4 5 6
7 8 9
#include <bits/stdc++.h>

using namespace std;

typedef vector< int > vi;

vi mat;
int N, L, idx;
string S;

vi press(vi a, vi b)
{
  for(int i = 0; i < N * N; i++) b[i] = a[b[i]];
  return (b);
}


vi fact(vi &v, int k)
{
  vi ret = mat;
  while(k > 0) {
    if(k & 1) ret = press(ret, v);
    v = press(v, v);
    k /= 2;
  }
  return (ret);
}

int num()
{
  int ret = 0;
  while(idx < S.size() && isdigit(S[idx])) {
    ret = ret * 10 + S[idx++] - '0';
  }
  return (ret);
}

vi sequence()
{
  if(idx == S.size() || S[idx] == ')') return (mat);
  vi ret = mat;
  if(S[idx] == '(') {
    ++idx;
    vi get = sequence();
    ++idx;
    ret = fact(get, num());
  } else if(S[idx] == 'L') {
    ++idx;
    int rep = num() - 1;
    for(int i = 0; i < N; i++) ret[rep * N + i] = rep * N + (i + 1) % N;
  } else if(S[idx] == 'R') {
    ++idx;
    int rep = num() - 1;
    for(int i = 0; i < N; i++) ret[rep * N + i] = rep * N + (i + N - 1) % N;
  } else if(S[idx] == 'U') {
    ++idx;
    int rep = num() - 1;
    for(int i = 0; i < N; i++) ret[i * N + rep] = (i + 1) % N * N + rep;
  } else {
    ++idx;
    int rep = num() - 1;
    for(int i = 0; i < N; i++) ret[i * N + rep] = (i + N - 1) % N * N + rep;
  }
  return (press(ret, sequence()));
}

int main()
{
  cin >> N >> L;
  cin >> S;

  mat.resize(N * N);
  iota(begin(mat), end(mat), 0);
  auto ret = sequence();

  for(int i = 0; i < N; i++) {
    for(int j = 0; j < N; j++) {
      if(j > 0) cout << " ";
      cout << ret[i * N + j] + 1;
    }
    cout << endl;
  }
}