#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int t, n, m, L = 20, a, e, y, z;
  char c;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    vector<int> b(n * m), r(n * m), s(n * m);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> c;
        b[i * m + j] = (c == '0');
      }
    }
    vector<vector<int> > p(n * m, vector<int>(L));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> c;
        if (c == 'U') p[i * m + j][0] = (i - 1) * m + j;
        if (c == 'R') p[i * m + j][0] = i * m + j + 1;
        if (c == 'D') p[i * m + j][0] = (i + 1) * m + j;
        if (c == 'L') p[i * m + j][0] = i * m + j - 1;
      }
    }
    for (int k = 1; k < L; k++)
      for (int i = 0; i < n * m; i++) p[i][k] = p[p[i][k - 1]][k - 1];
    for (int i = 0; i < n * m; i++) {
      a = i, e = n * m;
      for (int k = L - 1; k >= 0; k--)
        if (e >= (1 << k)) a = p[a][k], e -= (1 << k);
      r[a] = 1;
      if (b[i]) s[a] = 1;
    }
    y = 0, z = 0;
    for (int i = 0; i < n * m; i++) y += r[i], z += s[i];
    cout << y << " " << z << "\n";
  }
}
