#include <bits/stdc++.h>
char c[1000100] = {0};
char s[1000100] = {0};
int prev[1000100];
int dx[256] = {0};
int dy[256] = {0};
std::string sib = "DLRU";
int n, m, robots, blacks;
int nxt(int p) {
  int x = p % m + dx[s[p]];
  int y = p / m + dy[s[p]];
  return m * y + x;
}
int prev_d(int p, char d) {
  int x = p % m + dx[d];
  int y = p / m + dy[d];
  if (0 <= x && x < m && 0 <= y && y < n) return m * y + x;
  return -1;
}
const int flip[] = {0, 2, 1, 3};
const int bl[] = {0, 1, 1, 2};
template <class C, class B>
void rdfs(int u, int w, int l, int mod, B& b, C& level) {
  level[u] = 1;
  if (c[u] == '0' && !b[l]) {
    blacks++;
    b[l] = true;
  }
  for (auto d : sib) {
    int v = prev_d(u, d);
    if (v >= 0 && v != w && nxt(v) == u)
      rdfs(v, w, (l + mod - 1) % mod, mod, b, level);
  }
}
void proc() {
  std::cin >> n >> m;
  robots = blacks = 0;
  for (int i = 0; i < n; i++) std::cin >> &c[m * i];
  for (int i = 0; i < n; i++) std::cin >> &s[m * i];
  std::vector<int> level(n * m, false);
  for (int p = n * m, q, l; p--;) {
    if (level[p] != 0) continue;
    for (q = p, l = 0; level[q] == 0; q = nxt(q)) {
      prev[l] = q;
      level[q] = ++l;
    }
    int mod = l - level[q] + 1;
    robots += mod;
    std::vector<bool> b(mod, false);
    for (int i = mod; i--;)
      rdfs(prev[l - 1 - i], prev[l - 1 - (i + 1) % mod], mod - i - 1, mod, b,
           level);
  }
  std::cout << robots << ' ' << blacks << std::endl;
}
int main() {
  dx['L'] = -1;
  dx['R'] = +1;
  dy['U'] = -1;
  dy['D'] = +1;
  std::cin.tie(0);
  std::cin.sync_with_stdio(false);
  int t = 1;
  std::cin >> t;
  while (t--) proc();
  return 0;
}
