#include <bits/stdc++.h>
using namespace std;
double eps = (double)1 / 1e6;
long long inf = INT_MAX, mod2 = 998244353, mod1 = 1e9 + 7;
long long sqr(long long a) { return a * a; }
long long qb(long long a) { return a * a * a; }
long long gcd(long long a, long long b) { return !a ? b : gcd(b % a, a); }
long long binpow(long long a, long long b, long long mod) {
  return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod
                    : sqr(binpow(a, b / 2, mod)) % mod)
           : 1;
}
long long binmult(long long a, long long b, long long mod) {
  return b ? (b % 2 ? (2 * binmult(a, b / 2, mod) + a) % mod
                    : (2 * binmult(a, b / 2, mod)) % mod)
           : 0;
}
const long long R = 1e4;
const long long to[7] = {1, 2, 3, 1, 3, 2, 1};
const long long tx[4] = {0, 0, -1, 1};
const long long ty[4] = {-1, 1, 0, 0};
const char rev_to[4] = {'E', 'W', 'N', 'S'};
const long long N = 5000;
signed main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    vector<vector<char>> a(n, vector<char>(m));
    vector<vector<char>> b(n, vector<char>(m));
    vector<vector<pair<int, int>>> p(n, vector<pair<int, int>>(m));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> a[i][j];
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> b[i][j];
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int first = i, second = j;
        if (b[i][j] == 'U') first--;
        if (b[i][j] == 'D') first++;
        if (b[i][j] == 'L') second--;
        if (b[i][j] == 'R') second++;
        p[i][j] = make_pair(first, second);
      }
    }
    vector<vector<bool>> first(n, vector<bool>(m, 0));
    vector<vector<bool>> second(n, vector<bool>(m, 0));
    for (int k = 0; k < 23; k++) {
      vector<vector<pair<int, int>>> np(n, vector<pair<int, int>>(m));
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          np[i][j] = p[p[i][j].first][p[i][j].second];
        }
      }
      p = np;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (a[i][j] == '0') {
          second[p[i][j].first][p[i][j].second] = 1;
        }
        first[p[i][j].first][p[i][j].second] = 1;
      }
    }
    long long ps1 = 0, ps2 = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        (first[i][j] ? ps1++ : ps1);
        (second[i][j] ? ps2++ : ps2);
      }
    }
    cout << ps1 << " " << ps2 << "\n";
  }
  return 0;
}
