#include <bits/stdc++.h>
using namespace std;
int c[1000005];
int dp[1000005][22];
int ep[1000005];
void solve(int tc) {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < m; ++j) {
      c[m * i + j] = s[j] - '0';
    }
  }
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < m; ++j) {
      int u = m * i + j;
      if (s[j] == 'L') dp[u][0] = u - 1;
      if (s[j] == 'R') dp[u][0] = u + 1;
      if (s[j] == 'U') dp[u][0] = u - m;
      if (s[j] == 'D') dp[u][0] = u + m;
    }
  }
  for (int i = 1; i < 22; ++i)
    for (int j = 0; j < m * n; ++j) dp[j][i] = dp[dp[j][i - 1]][i - 1];
  for (int i = 0; i < m * n; ++i) {
    ep[i] = -1;
  }
  for (int i = 0; i < m * n; ++i) {
    int v = dp[i][21];
    if (c[i] == 0)
      ep[v] = 0;
    else if (ep[v] == -1)
      ep[v] = 1;
  }
  int total = 0, black = 0;
  for (int i = 0; i < m * n; ++i) {
    if (ep[i] != -1) {
      ++total;
      if (ep[i] == 0) ++black;
    }
  }
  cout << total << " " << black << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  cin >> t;
  for (int tc = 1; tc <= t; ++tc) {
    solve(tc);
  }
  return 0;
}
