Example

Input

3 2
R1


Output

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

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++)

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

using pint = pair<int, int>;
using tint = tuple<int, int, int>;
using vint = vector<int>;
using mat = vector<vint>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

string ope = "LRUD";

int N, L;
string S;

int idx = 0;

mat mat_calc(mat);
mat mat_rept(mat);

mat mat_init() {
  mat res(N, vector<int>(N));
  rep(i, N) rep(j, N) res[i][j] = N*i+j;
  return res;
}

void mat_print(mat mtrx) {
  rep(i, N) rep(j, N) cout << mtrx[i][j]+1 << (j==N-1 ? '\n' : ' ');
}

int getNum() {
  int res = 0;
  for(; idx < L && isdigit(S[idx]); idx++) res = res*10 + (S[idx]-'0');
  return res;
}

mat mat_shift(mat mtrx) {
  char shift = S[idx++];
  int num = getNum(); num--;
  if(shift == 'L') {
    int tmp = mtrx[num][0];
    for(int j = 0; j < N-1; j++) mtrx[num][j] = mtrx[num][j+1];
    mtrx[num][N-1] = tmp;
  } else if(shift == 'R') {
    int tmp = mtrx[num][N-1];
    for(int j = N-1; j > 0; j--) mtrx[num][j] = mtrx[num][j-1];
    mtrx[num][0] = tmp;
  } else if(shift == 'U') {
    int tmp = mtrx[0][num];
    for(int j = 0; j < N-1; j++) mtrx[j][num] = mtrx[j+1][num];
    mtrx[N-1][num] = tmp;
  } else if(shift == 'D') {
    int tmp = mtrx[N-1][num];
    for(int j = N-1; j > 0; j--) mtrx[j][num] = mtrx[j-1][num];
    mtrx[0][num] = tmp;
  } else {
    assert(false);
  }
  return mtrx;
}

mat mat_calc(mat mtrx) {
  if(ope.find(S[idx]) != string::npos) mtrx = mat_shift(mtrx);
  else if(S[idx] == '(') mtrx = mat_rept(mtrx);
  else assert(false);
  return mtrx;
}

mat mat_rept(mat mtrx) {
  idx++; // (
  mat res = mat_init();
  mat tmp = mat_init();
  while(S[idx] != ')') tmp = mat_calc(tmp);
  idx++; // )
  int num = getNum();
  for(int i = 0; (num>>i); i++) {
    if((num>>i)&1) {
      rep(i, N) rep(j, N) res[i][j] = tmp[res[i][j]/N][res[i][j]%N];
    }
    mat tmp2 = tmp;
    rep(i, N) rep(j, N) tmp[i][j] = tmp2[tmp[i][j]/N][tmp[i][j]%N];
  }
  mat tmp3 = mtrx;
  rep(i, N) rep(j, N) mtrx[i][j] = tmp3[res[i][j]/N][res[i][j]%N];

  return mtrx;
}

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin >> N >> L >> S;
  mat mtrx = mat_init();
  while(idx < L) mtrx = mat_calc(mtrx);
  mat_print(mtrx);

  return 0;
}