#include <bits/stdc++.h>
using namespace std;
const long long int inf = 9e18;
int t, n, m, temp;
vector<vector<int>> dp(1e6 + 1, vector<int>(25, 0));
vector<string> arr, brr;
set<int> st, st1;
void solve() {
  arr.clear(), brr.clear(), st.clear(), st1.clear();
  cin >> n >> m;
  arr.resize(n), brr.resize(n);
  ;
  for (int i = 0; i < n; i++) cin >> brr[i];
  ;
  for (int i = 0; i < n; i++) cin >> arr[i];
  ;
  for (int i = 0; i < n; i++) {
    ;
    for (int j = 0; j < m; j++) {
      if (arr[i][j] == 'R')
        dp[i * m + j][0] = i * m + j + 1;
      else if (arr[i][j] == 'L')
        dp[i * m + j][0] = i * m + j - 1;
      else if (arr[i][j] == 'U')
        dp[i * m + j][0] = (i - 1) * m + j;
      else
        dp[i * m + j][0] = (i + 1) * m + j;
    }
  };
  for (int i = 1; i < 25; i++) {
    ;
    for (int j = 0; j < n * m; j++) dp[j][i] = dp[dp[j][i - 1]][i - 1];
  };
  for (int i = 0; i < n * m; i++) {
    st.insert(dp[i][24]);
    if (brr[i / m][i % m] == '0') st1.insert(dp[i][24]);
  }
  cout << st.size() << " " << st1.size() << "\n";
}
int32_t main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> t;
  while (t--) {
    solve();
  }
}
