Example

Input

3 2
R1


Output

3 1 2
4 5 6
7 8 9
#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)

struct Parser{
    using Iter=string::const_iterator;
    Iter it;
    Parser(Iter it):it(it){}
    void cmp(char expected){
        if(*it!=expected){
            throw 0;
            cerr<<*it<<" is not "<<expected<<endl;
        }
    }

    vector<vector<vector<int>>> sub;

    // LRUD
    string LRUD = "LRUD";
    const int di[4] = {0, 0, 1, -1};
    const int dj[4] = {1, -1, 0, 0};
    int id[128];

    int N;
    int NN;
    void init(int n){
        N = n;
        NN = N * N;
        sub.assign(4, vector<vector<int>>(N, vector<int>(NN)));
        for(int i = 0; i < 4; i++)
            id[LRUD[i]] = i;
        rep(d, 0, 4) {
            rep(k,0,n){
                rep(i, 0, n)rep(j,0,n){
                    if((d<2&&i==k)||(d>=2&&j==k)){
                        int ni = (i + di[d] + n) % n, nj = (j + dj[d] + n) % n;
                        sub[d][k][i*n+j]=ni*n+nj;
                   }else
                        sub[d][k][i*n+j]=i*n+j;
                }
            }
        }
    }

    vector<int> mul(const vector<int> &a, const vector<int> &b) {
        vector<int> r(NN);
        rep(i,0,NN){
            r[i] = a[b[i]];
        }
        return r;
    }

    vector<int> seq(){
        vector<int> v(NN);
        iota(v.begin(),v.end(),0);
        while(*it!='$' && *it!=')'){
            if(*it=='('){
                it++;
                vector<int> res = seq();
                it++;
                int num=integer();
                //行列累乗
                vector<int> a = res;
                vector<int> r(NN); iota(r.begin(), r.end(), 0);
                while(num) {
                    if(num&1)r=mul(r,a);
                    a=mul(a,a);
                    num>>=1;
                }
                v=mul(v,r);
            }else{
                char shift=*it;
                it++;
                int num=integer()-1;
                v=mul(v,sub[id[shift]][num]);
            }
        }
        return v;
    }
    int integer(){
        int ret=0;
        while(isdigit(*it)){
            ret*=10;
            ret+=*it-'0';
            it++;
        }
        return ret;
    }
};

signed main(){
    int N, L; cin>>N>>L;
    string S;cin>>S;S+="$";
    Parser p(S.begin());
    p.init(N);
    vector<int> res = p.seq();
    rep(i,0,N) {
        rep(j,0,N){
            if(j!=0)cout<<" ";
            cout<<res[i*N+j]+1;
        }
        cout<<endl;
    }
    return 0;
}
