#include <iostream>
#include <string>
#include <vector>
#include <random>
#include <stdlib.h>
#include <time.h>
#include <utility>
#include <algorithm>
#include <unistd.h>
using namespace std;


int main(){
  srand((unsigned)time(NULL));
  time_t start = time(NULL);
  int si,sj,x,y,now,cnt,num=0;
  string ans="",A="";
  cin>>si>>sj;
  vector<vector<int>> t(50,vector<int>(50));
  vector<vector<int>> p(50,vector<int>(50));
  vector<int> gone(2500);
  for(int i=0;i<50;i++)for(int j=0;j<50;j++)cin>>t.at(i).at(j);
  for(int i=0;i<50;i++)for(int j=0;j<50;j++){
    cin>>p.at(i).at(j);
  }
  while(time(NULL)-start<1.9){
    x=si;//初期化
    y=sj;
    ans="";
    cnt=p.at(x).at(y);
    fill(gone.begin(),gone.end(),0);
    while(1){
      now=t.at(x).at(y);
      gone.at(now)=1;
      vector<pair<pair<int,char>,pair<int,int>>> tmp;
      if(x!=0 && gone.at(t.at(x-1).at(y))==0){
        tmp.push_back({{p.at(x-1).at(y),'U'},{x-1,y}});
      }
      if(x!=49 && gone.at(t.at(x+1).at(y))==0){
        tmp.push_back({{p.at(x+1).at(y),'D',},{x+1,y}});
      }
      if(y!=0 && gone.at(t.at(x).at(y-1))==0){
        tmp.push_back({{p.at(x).at(y-1),'L'},{x,y-1}});
      }
      if(y!=49 && gone.at(t.at(x).at(y+1))==0){
        tmp.push_back({{p.at(x).at(y+1),'R'},{x,y+1}});
      }
      if(tmp.size()==0){
        if(cnt>num){
          num=cnt;
          A=ans;
        }
        break;
      }
      int len=tmp.size();
      int go;
      if(len==1){
        go=0;
      }else if(len==2){
        sort(tmp.begin(),tmp.end());
        int q=rand()%10;
        if(q<2)go=0;
        else go=1;
      }else if(len==3){
        sort(tmp.begin(),tmp.end());
        int q=rand()%10;
        if(q<1)go=0;
        else if(q<3)go=1;
        else go=2;
      }else{
        sort(tmp.begin(),tmp.end());
        //最初だけありえる
        go=rand()%4;
      }
      if(go>=len){
        sleep(2);
      }
      pair<pair<int,char>,pair<int,int>> u=tmp.at(go);
      x=u.second.first;
      y=u.second.second;
      ans+=u.first.second;
      cnt+=u.first.first;
    }
    //cout<<cnt<<endl;
  }
  cout<<A<<endl;
  //cerr<<time(NULL)-start<<endl;
  return 0;
}
#include <bits/stdc++.h>

using namespace std;

#define DIM 20
#define DIMMASK 270007

//#define ll long long

long long a[DIM],d[DIMMASK];

bool isbit(int mask, int bit) {
    return (mask & (1 << (bit-1))) != 0;
}

int removebit(int mask, int b) {
    return mask & ~(1 << (b-1));
}

vector< pair<int,int> > g[DIM];
int n,q,x,y,z;

int main()
{
    //freopen("input.txt","r",stdin);

    int n,maxmask,prevmask,currres,currcount;

    cin >> n >> q;
    for(int i=1; i<=q; i++) {
        cin >> x >> y >> z;
        g[x].push_back({y,z});
    }

    d[0] = 1;
    maxmask = (1 << n) - 1;
    for(int m = 0; m <= maxmask; m++) {
        for(int last = 1; last <= n; last++) {
            if(!isbit(m,last)) continue;
            prevmask = removebit(m,last);
            d[m] += d[prevmask];
        }

        //cout <<"BEFORE "<< m<<" "<<d[m]<<endl;

        x = __builtin_popcount(m);
        for(auto [y,z] : g[x]) {
            //cout <<"YZ="<<y<<" "<<z<<endl;
            int tmpmask = m & ((1<<y)-1);
            //cout <<"PC="<<__builtin_popcount(tmpmask)<<endl;
            if(__builtin_popcount(tmpmask)>z) d[m] = 0;
        }
        //cout <<"AFTER "<< m<<" "<<d[m]<<endl;

    }

    cout << d[maxmask] << endl;
    //for(int i=0; i<=maxmask; i++) cout << i<<" "<<d[i]<<endl;

    return 0;
}
