#include <bits/stdc++.h>
using namespace std;
string s;
const int N = 1e6;
int t, m, n;
int val[N], res[N], ans[N];
int f[N][22];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> m >> n;
    for (int i = 0; i < m; i++) {
      cin >> s;
      for (int j = 0; j < n; j++) val[i * n + j] = (s[j] - '0') ^ 1;
    }
    for (int i = 0; i < m; i++) {
      cin >> s;
      for (int j = 0; j < n; j++) {
        int x = i, y = j;
        if (s[j] == 'U') x--;
        if (s[j] == 'D') x++;
        if (s[j] == 'L') y--;
        if (s[j] == 'R') y++;
        f[i * n + j][0] = x * n + y;
      }
    }
    for (int d = 1; d <= 21; d++)
      for (int i = 0; i < m * n; i++) f[i][d] = f[f[i][d - 1]][d - 1];
    for (int i = 0; i < m * n; i++) {
      ans[f[i][21]]++;
      res[f[i][21]] += val[i];
    }
    int R1 = 0, R2 = 0;
    for (int i = 0; i < m * n; i++) {
      R1 += (ans[i] > 0);
      R2 += (res[i] > 0);
    }
    for (int i = 0; i < m * n; i++) ans[i] = res[i] = 0;
    cout << R1 << ' ' << R2 << '\n';
  }
}
