#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
int t, n, m;
int nxt[1000000];
int cnt[1000000];
int q[1000000];
int b[1000000];
int dp[2][1000000];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n >> m;
    string s;
    for (int i = 0; i < n; ++i) {
      cin >> s;
      for (int j = 0; j < m; ++j) {
        b[i * m + j] = s[j] == '0';
      }
    }
    for (int i = 0; i < n; ++i) {
      cin >> s;
      for (int j = 0; j < m; ++j) {
        int nx = -1;
        if (s[j] == 'U') {
          nx = (i - 1) * m + j;
        }
        if (s[j] == 'D') {
          nx = (i + 1) * m + j;
        }
        if (s[j] == 'L') {
          nx = i * m + j - 1;
        }
        if (s[j] == 'R') {
          nx = i * m + j + 1;
        }
        nxt[i * m + j] = nx;
      }
    }
    int sz = n * m;
    for (int i = 0; i < sz; ++i) {
      dp[0][i] = nxt[i];
    }
    for (int i = 0; i < 20; ++i) {
      for (int j = 0; j < sz; ++j) {
        dp[1][j] = dp[0][dp[0][j]];
      }
      for (int j = 0; j < sz; ++j) {
        dp[0][j] = dp[1][j];
      }
    }
    fill(cnt, cnt + sz, 0);
    fill(q, q + sz, 0);
    for (int i = 0; i < sz; ++i) {
      ++cnt[dp[0][i]];
      q[dp[0][i]] += b[i];
    }
    int ans1 = 0, ans2 = 0;
    for (int i = 0; i < sz; ++i) {
      ans1 += (int)(cnt[i] > 0);
      ans2 += (int)(q[i] > 0);
    }
    cout << ans1 << ' ' << ans2 << endl;
  }
  return 0;
}
