#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
const long long MOD = 1e9 + 7;
const long long INF = 0x3f3f3f3f;
long long read() {
  long long x = 0, f = 1;
  char ch;
  do {
    ch = getchar();
    if (ch == '-') f = -1;
  } while (ch < '0' || ch > '9');
  do {
    x = x * 10 + ch - 48;
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  return x * f;
}
int N, M;
bool black[MAXN];
char mp[MAXN];
int val[MAXN];
int nxt[MAXN][30];
char goes[10] = "LURD";
int change[10];
int ans1, ans2;
int id(int i, int j) { return i * M + j; }
void init() {
  for (int i = N * M; i >= 0; i--) {
    val[i] = 0;
  }
  change[0] = -1;
  change[1] = -M;
  change[2] = 1;
  change[3] = M;
  ans1 = ans2 = 0;
  for (int i = N * M - 1; i >= 0; i--) {
    for (int j = 0; j < 4; j++) {
      if (mp[i] == goes[j]) {
        nxt[i][0] = i + change[j];
        break;
      }
    }
  }
}
void solve() {
  int x, y;
  int now;
  scanf("%d%d", &N, &M);
  getchar();
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      black[id(i, j)] = (getchar() == '0');
    }
    getchar();
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      mp[id(i, j)] = getchar();
    }
    getchar();
  }
  init();
  for (int j = 0; j < 20; j++) {
    for (int i = N * M - 1; i >= 0; i--) {
      nxt[i][j + 1] = nxt[nxt[i][j]][j];
    }
  }
  for (int i = N * M - 1; i >= 0; i--) {
    val[nxt[i][20]] |= 2 | black[i];
  }
  for (int i = N * M - 1; i >= 0; i--) {
    ans1 += (bool)val[i];
    ans2 += (val[i] & 1);
  }
  printf("%d %d\n", ans1, ans2);
}
int main() {
  int T;
  T = 1;
  scanf("%d", &T);
  while (T--) {
    solve();
  }
  return 0;
}
