#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int mod = 1e9 + 7;
int add(int a, int b) {
    return (a + b) % mod;
}
int mul(int a, int b) {
    ll ans = a * 1ll * b;
    return ans % mod;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int h, w;
    cin >> h >> w;
    vector<vector<bool>> grid(h, vector<bool>(w, false));
    int cnt = 0;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            char c;
            cin >> c;
            if (c == '.') {
                grid[i][j] = true;
                cnt++;
            }
        }
    }
    int res = 0;
    vector<int> pow(cnt + 1);
    pow[0] = 1;
    for (int i = 1; i <= cnt; i++) {
        pow[i] = add(pow[i - 1], pow[i - 1]);
    }
    vector<vector<pair<int, int>>> gridn(h, vector<pair<int, int>>(w, make_pair(0, 0)));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (grid[i][j]) {
                if (gridn[i][j].first == 0) {
                    int temp = 1;
                    for (int k = j + 1; k < w; k++) {
                        if (!grid[i][k]) {
                            break;
                        }
                        temp++;
                    }
                    //cout << "asd" << temp << endl;
                    for (int k = j; k < w; k++) {
                        if (!grid[i][k]) {
                            break;
                        }
                        gridn[i][k].first = temp;
                    }
                }
                if (gridn[i][j].second == 0) {
                    int temp = 1;
                    for (int k = i + 1; k < h; k++) {
                        if (!grid[k][j]) {
                            break;
                        }
                        temp++;
                    }
                    //cout << "asdf" << temp << endl;
                    for (int k = i; k < h; k++) {
                        if (!grid[k][j]) {
                            break;
                        }
                        gridn[k][j].second = temp;
                    }
                }
                int temp = gridn[i][j].first + gridn[i][j].second - 1;
                //cout << temp << i << j << endl;
                res = add(res, mul(pow[cnt - temp], pow[temp] - 1));
            }
        }
    }
    cout << res << endl;
    return 0;
}
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <numeric>
#include <iomanip>
#include <algorithm>
using namespace std;

string rus;
int point = 0;

void move(vector<vector<int>> &mdata, vector<vector<int>> &pdata, vector<int> cdata,int x,int y,int p,int dir, string sdata,int c){
	if (point < p) {
		rus = sdata;
		point = p;
	}
	if (x > 50 || y > 50 || x <= 0 || y <= 0) {
		return;
	}
	if (cdata.at(mdata.at(y).at(x)) == 0) {
		return;
	}
	cdata.at(mdata.at(y).at(x)) = 0;
	p = p + pdata.at(y).at(x);

	if (dir == 1)
		sdata += 'D';
	else if (dir == 2)
		sdata += 'U';
	else if (dir == 3)
		sdata += 'R';
	else if (dir == 4)
		sdata += 'L';

	map<int, int> hp;
	int cnt = 0;
	if(cdata.at(mdata.at(y + 1).at(x)) != 0)
		hp[pdata.at(y + 1).at(x)] = 1;
	if (cdata.at(mdata.at(y - 1).at(x)) != 0)
		hp[pdata.at(y - 1).at(x)] = 2;
	if (cdata.at(mdata.at(y).at(x + 1)) != 0)
		hp[pdata.at(y).at(x + 1)] = 3;
	if (cdata.at(mdata.at(y).at(x - 1)) != 0)
		hp[pdata.at(y).at(x - 1)] = 4;
	
	for (auto i = hp.rbegin();i != hp.rend();i++) {
		if (i->second == 1) {
			move(mdata, pdata, cdata, x, y + 1, p, 1, sdata,c+1);
		}
		else if (i->second == 2) {
			move(mdata, pdata, cdata, x, y - 1, p, 2, sdata,c+1);
		}
		else if (i->second == 3) {
			move(mdata, pdata, cdata, x + 1, y, p, 3, sdata,c+1);
		}
		else {
			move(mdata, pdata, cdata, x - 1, y, p, 4, sdata,c+1);
		}

		//cout << i->second << endl;
		cnt++;
		if ((cnt == 1 && c > 15) ||(cnt == 2 && c > 10)) break;
	}
}

int main(void) {
	int sx, sy;
	string sdata;

	cin >> sy >> sx;

	vector<int> cdata(52 * 52, 0);
	vector<vector<int>> mdata(52, vector<int>(52));
	vector<vector<int>> pdata(52, vector<int>(52));
	for (int i = 1;i <= 50;i++) {
		for (int j = 1;j <= 50;j++) {
			cin >> mdata.at(i).at(j);
			cdata.at(mdata.at(i).at(j)) = 1;
		}
	}
	for (int i = 1;i <= 50;i++) {
		for (int j = 1;j <= 50;j++) {
			cin >> pdata.at(i).at(j);
		}
	}
	move(mdata,pdata, cdata, sx+1, sy+1,pdata.at(sy+1).at(sx+1),0,sdata,0);

	cout << rus << endl;
}