#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 2e6 + 10;
int cir;
vector<int> vs;
vector<vector<pair<int, int>>> cis;
string s[maxn];
map<char, pair<int, int>> mc;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
char ch[] = {'U', 'R', 'D', 'L'};
long long ans1, ans2;
stack<pair<int, int>> st;
set<int> sp;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  for (int i = (0); i < (4); ++i) mc[ch[i]] = make_pair(dx[i], dy[i]);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    cir = 0;
    vs.clear();
    cis.clear();
    ans1 = ans2 = 0;
    int f[n + 1][m + 1];
    int ci[n + 1][m + 1];
    int bw[n + 1][m + 1];
    memset(f, -1, sizeof(f));
    memset(ci, 0, sizeof(ci));
    for (int i = (0); i < (n); ++i) cin >> s[i];
    for (int i = (0); i < (n); ++i) {
      for (int j = (0); j < (m); ++j) {
        bw[i][j] = s[i][j] - '0';
      }
    }
    for (int i = (0); i < (n); ++i) cin >> s[i];
    for (int i = (0); i < (n); ++i)
      for (int j = (0); j < (m); ++j)
        if (f[i][j] == -1) {
          ++cir;
          int x = i, y = j;
          while (f[x][y] != cir && f[x][y] <= 0) {
            pair<int, int> it = mc[s[x][y]];
            st.push(make_pair(x, y));
            f[x][y] = cir;
            x += it.first, y += it.second;
          }
          if (f[x][y] == cir) {
            cis.push_back({});
            int ex, ey, q = 0, es = 0;
            do {
              ex = st.top().first, ey = st.top().second;
              ci[ex][ey] = q++;
              st.pop();
              cis[cir - 1].push_back(make_pair(ex, ey));
            } while (ex != x || ey != y);
            while (!st.empty()) {
              ex = st.top().first, ey = st.top().second;
              ci[ex][ey] = es;
              es = (es + 1) % q;
              st.pop();
              cis[cir - 1].push_back(make_pair(ex, ey));
            }
            vs.push_back(q);
            ans1 += q;
          } else {
            --cir;
            int now = f[x][y], q = vs[f[x][y] - 1], es = ci[x][y];
            int ex, ey;
            while (!st.empty()) {
              ex = st.top().first, ey = st.top().second;
              f[ex][ey] = now;
              es = (es + 1) % q;
              ci[ex][ey] = es;
              cis[now - 1].push_back(make_pair(ex, ey));
              st.pop();
            }
          }
        }
    for (int i = (0); i < (cir); ++i) {
      sp.clear();
      for (auto it : cis[i]) {
        if (!bw[it.first][it.second]) sp.insert(ci[it.first][it.second]);
      }
      ans2 += (int)(sp).size();
    }
    cout << ans1 << ' ' << ans2 << '\n';
  }
  return 0;
}
