#include <bits/stdc++.h>
using namespace std;
const long double PI = atan(1.0) * 4;
const int64_t INF = INT64_MAX;
const int32_t LOG = 21;
const int MOD = 1e9 + 7;
int n, m;
inline int get(int x, int y) { return (x - 1) * m + y; }
int val[1000005];
char ch[1000005];
bool check[1000005];
bool check1[1000005];
map<char, pair<int, int> > map1;
int par[1000005][25];
int32_t main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  int tests = 1;
  cin >> tests;
  map1['R'] = {0, 1};
  map1['L'] = {0, -1};
  map1['U'] = {-1, 0};
  map1['D'] = {1, 0};
  for (int ii = 0; ii < tests; ii++) {
    cin >> n >> m;
    int tot = n * m;
    fill(check, check + tot + 2, false);
    fill(check1, check1 + tot + 2, false);
    for (int i = 1; i < n + 1; i++) {
      string second;
      cin >> second;
      for (int j = 1; j < m + 1; j++) val[get(i, j)] = second[j - 1] - '0';
    }
    for (int i = 1; i < n + 1; i++) {
      string second;
      cin >> second;
      for (int j = 1; j < m + 1; j++) ch[get(i, j)] = second[j - 1];
    }
    for (int i = 1; i < n + 1; i++) {
      for (int j = 1; j < m + 1; j++) {
        par[get(i, j)][0] = get(i, j) + m * map1[ch[get(i, j)]].first +
                            map1[ch[get(i, j)]].second;
      }
    }
    for (int it = 1; it < 22; it++) {
      for (int i = 1; i < tot + 1; i++) {
        int temp = par[i][it - 1];
        par[i][it] = par[temp][it - 1];
      }
    }
    int ans = 0, black = 0;
    for (int i = 1; i < tot + 1; i++) {
      int x = par[i][21];
      if (!check[x]) {
        check[x] = true;
        ans++;
      }
      if (!check1[x] && val[i] == 0) {
        check1[x] = true;
        black++;
      }
    }
    cout << ans << " " << black << '\n';
  }
  return 0;
}
