#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
char a[N];
int vis[N], dp[N];
vector<int> node[N], prvnode[N];
stack<int> s;
unordered_map<int, int> onstack;
bool flag = false;
vector<int> cyc_node;
void find_cycle(int u) {
  s.push(u);
  onstack[u] = 1;
  for (auto j : node[u]) {
    if (onstack[j] == 1) {
      while (true) {
        cyc_node.push_back(s.top());
        if (s.top() == j)
          break;
        else
          s.pop();
      }
      flag = true;
      return;
    } else {
      find_cycle(j);
      if (flag) return;
    }
  }
  onstack[u] = 0;
  s.pop();
}
struct viet {
  int now, id, from;
};
int bfs() {
  queue<viet> q;
  int cnt = 0;
  for (auto j : cyc_node) {
    q.push({j, cnt, j});
    cnt++;
    vis[j] = 1;
  }
  set<int> mark;
  while (q.size()) {
    viet x = q.front();
    if (a[x.now] == '0') mark.insert(x.id);
    q.pop();
    for (auto j : prvnode[x.now]) {
      if (vis[j]) continue;
      vis[j] = 1;
      int mod = cyc_node.size();
      q.push({j, (x.id + 1) % mod, x.from});
    }
  }
  int rev = mark.size();
  return rev;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  if (fopen("password.inp", "r")) {
    freopen("password.inp", "r", stdin);
    freopen("password.out", "w", stdout);
  }
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m * n; i++) {
      node[i].clear();
      prvnode[i].clear();
      vis[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        int now = m * (i - 1) + j;
        cin >> a[now];
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        char task;
        cin >> task;
        int now = (i - 1) * m + j;
        if (task == 'U') {
          node[now].push_back(now - m);
          prvnode[now - m].push_back(now);
        }
        if (task == 'D') {
          node[now].push_back(now + m);
          prvnode[now + m].push_back(now);
        }
        if (task == 'R') {
          node[now].push_back(now + 1);
          prvnode[now + 1].push_back(now);
        }
        if (task == 'L') {
          node[now].push_back(now - 1);
          prvnode[now - 1].push_back(now);
        }
      }
    }
    int ans = 0, bestblack = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        int now = (i - 1) * m + j;
        if (vis[now] == 0) {
          flag = false;
          onstack.clear();
          cyc_node.clear();
          find_cycle(now);
          ans += cyc_node.size();
          bestblack += bfs();
        }
      }
    }
    cout << ans << " " << bestblack << '\n';
  }
}
