#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define sz(v) (int)v.size()
#define precision cout << fixed << setprecision(15);

const int inf = 1e9;
const long long INF = 1e18;
const int mod = 1e9 + 7;
const int bit32 = log2(inf) + 3;
const int bit64 = log2(INF) + 3;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
clock_t time_p = clock();

void ktj() {
  time_p = clock() - time_p;
  cerr << "Time elapsed : " << (float)(time_p)/CLOCKS_PER_SEC << "\n";
}

void pre() {}

const int N = 2e5 + 5;
int a[N], b[N], order[N];
int bit[N];

void update(int idx, int val){
  for(; idx <= N; idx += (idx & (-idx))){
    bit[idx] += val;
  }
}

int query(int idx){
  int sum = 0;
  for(; idx > 0; idx -= (idx & (-idx))){
    sum += bit[idx];
  }
  return sum;
}

string to_string(string s) { return '"' + s + '"';}
string to_string(char s) { return string(1, s);}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
template <typename A> string to_string(A);
template <typename A, typename B>string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool f = 1; string r = "{"; 
  for (const auto &x : v) {
    if (!f)r += ", "; f = 0; r += to_string(x);
  }
  return r + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H); debug_out(T...);
}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

void solve() {
  int h, w, m, i, j, k;
  cin >> h >> w >> m;

  for (i = 1; i <= h; i++) 
    a[i] = w;
  for (i = 1; i <= w; i++)
    b[i] = h;

  for (i = 0; i < m; i++) {
    int x, y;  
    cin >> x >> y;
    a[x] = min(a[x], y - 1);
    b[y] = min(b[y], x - 1);
  }

  long long ans = 0;

  for (i = 1; i <= b[1]; i++) {
    ans += a[i];
  }

  for (i = 1; i <= a[1]; i++) {
    order[i] = i;
  }

  sort(order + 1, order + a[1] + 1, [&](int x, int y) {
    return b[x] < b[y];
  });

  order[0] = 0;
  b[0] = 0;

  for (i = 1; i <= a[1]; i++) {
    for (j = b[order[i - 1]] + 1; j <= b[order[i]] and j <= b[1]; j++) {
      update(a[j], 1);
    }
    ans += b[order[i]] - (query(N) - query(order[i] - 1));
  }

  cout << ans << '\n';
}

#define GOOGLE 0
#define MULTIPLE_TC 0

int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);cout.tie(NULL); 

  pre();

  int t = 1, T;

  if (MULTIPLE_TC)
    cin >> t;

  for (T = 1; T <= t; T++) {
    if (GOOGLE)
      cout << "Case #" << T << ": ";
    solve();
  }

  ktj();
}

#include <bits/stdc++.h>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll I=1167167167167167167;
ll Q=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end());reverse(v.begin(),v.end());}
template<class T> void print_tate(vector<T> &v) {rep(i,v.size()) cout<<v[i]<<"\n";}
void yneos(bool a){if(a) cout<<"Yes"<<"\n"; else cout<<"No"<<"\n";}

int N=50,M=0;
int start_x,start_y;
vector<vector<int>> tile(N,vector<int>(N));
vector<vector<int>> wrote_point(N,vector<int>(N));
vector<int> point;
int start_tile;

vector<int> vari_x={0,0,-1,1},vari_y={-1,1,0,0};

struct date
{
	string route;
	int x;
	int y;
	int now_point;
	set<int> arrived;
	int rate;
	 bool operator<(const date& another) const {
        //メンバ変数であるnum1で比較した結果を
        //この構造体の比較とする
        return now_point < another.now_point;
    }
	 bool operator>(const date& another) const {
        //メンバ変数であるnum1で比較した結果を
        //この構造体の比較とする
        return now_point > another.now_point;
    }
};

int value(int a){
	return a;
}

bool check(int x,int y){
	return (0<=x&&x<N&&0<=y&&y<N);
}

date add(date a,int x,int y){//add_checkを通過していること
	if(x-a.x==1) a.route+="D";
	else if(x-a.x==-1) a.route+="U";
	else if(y-a.y==1) a.route+="R";
	else a.route+="L";
	a.arrived.insert(tile[x][y]);
	a.now_point+=point[tile[x][y]];
	a.x=x;
	a.y=y;
	return a;
}

bool add_check(date &a,int X,int Y){
	if(!check(X,Y)) return false;
	if(a.arrived.count(tile[X][Y])) return false;
	if(abs(a.x-X)+abs(a.y-Y)!=1) return false;
	return true;
}


void solve(){
	int range=30;
	date ans={"",start_x,start_y,point[start_tile],{start_tile},point[start_tile]};
	vector<date> looking={ans};
	while(looking.size()!=0&&clock()<1.9*CLOCKS_PER_SEC){
		vector<date> next_looking;
		ans=looking[0];
		rep(i,min(range,(int)looking.size())){
			rep(j,4){
				int s=looking[i].x+vari_x[j],t=looking[i].y+vari_y[j];
				if(add_check(looking[i],s,t)){
					next_looking.push_back(add(looking[i],s,t));
				}
			}
		}
		if(next_looking.size()==0) break;
		Sore(next_looking);
		looking=next_looking;
	}
	cout<<looking[0].route<<endl;
}

//main関数だよーーー
int main() {
	cin>>start_x>>start_y;
	rep(i,N) rep(j,N) cin>>tile[i][j],chmax(M,tile[i][j]);
	rep(i,N) rep(j,N) cin>>wrote_point[i][j];
	M++;
	point.resize(M);
	rep(i,N) rep(j,N) point[tile[i][j]]+=wrote_point[i][j];
	start_tile=tile[start_x][start_y];
	solve();
}
