#include <bits/stdc++.h>
using namespace std;
int dx[300], dy[300];
int m;
int f(int i, int j) { return i * m + j; }
int X[21][1000000];
int main() {
  dx['U'] = -1, dy['U'] = 0;
  dx['D'] = 1, dy['D'] = 0;
  dx['L'] = 0, dy['L'] = -1;
  dx['R'] = 0, dy['R'] = 1;
  int tc;
  cin >> tc;
  while (tc--) {
    int n;
    cin >> n >> m;
    vector<string> c(n), d(n);
    for (int i = 0; i < n; i++) cin >> c[i];
    for (int i = 0; i < n; i++) cin >> d[i];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        X[0][f(i, j)] = f(i + dx[d[i][j]], j + dy[d[i][j]]);
    int T = ceil(log2(n * m));
    for (int i = 1; i <= T; i++)
      for (int j = 0; j < n * m; j++) X[i][j] = X[i - 1][X[i - 1][j]];
    vector<int> p(n * m, 0), q(n * m, 0);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        q[X[T][f(i, j)]] = 1;
        if (c[i][j] == '0') p[X[T][f(i, j)]] = 1;
      }
    }
    int r1 = 0, r2 = 0;
    for (int i = 0; i < n * m; i++) r1 += q[i], r2 += p[i];
    cout << r1 << " " << r2 << endl;
  }
  return 0;
}
