#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000 + 10;
inline long long read() {
  char ch = getchar();
  long long x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int black[maxn], white[maxn];
int lca[maxn][24];
int color[maxn];
char s[maxn];
vector<int> v[maxn];
int n, m;
void init() {
  for (int i = 1; i <= n * m; ++i) v[i].clear();
}
int main() {
  int T = read();
  while (T--) {
    n = read(), m = read();
    init();
    for (int i = 1; i <= n; ++i) {
      scanf("%s", s + 1);
      for (int j = 1; j <= m; ++j) {
        int now = (i - 1) * m + j;
        color[now] = s[j] - '0';
      }
    }
    for (int i = 1; i <= n; ++i) {
      scanf("%s", s + 1);
      for (int j = 1; j <= m; ++j) {
        int now = (i - 1) * m + j;
        if (s[j] == 'U')
          lca[now][0] = now - m;
        else if (s[j] == 'D')
          lca[now][0] = now + m;
        else if (s[j] == 'L')
          lca[now][0] = now - 1;
        else
          lca[now][0] = now + 1;
      }
    }
    n *= m;
    for (int i = 1; i <= 20; ++i)
      for (int j = 1; j <= n; ++j) lca[j][i] = lca[lca[j][i - 1]][i - 1];
    for (int j = 1; j <= n; ++j) {
      int to = j;
      for (int i = 20; i >= 0; --i) {
        if ((1 << i) & n) to = lca[to][i];
      }
      v[to].push_back(j);
    }
    int ans = 0;
    int black_num = 0;
    for (int i = 1; i <= n; ++i) {
      if (v[i].size() != 0) {
        ans++;
        int sz = v[i].size();
        bool ok = false;
        for (int j = 0; j < sz; ++j) {
          if (color[v[i][j]] == 0) {
            ok = true;
            break;
          }
        }
        if (ok) black_num++;
      }
    }
    printf("%d %d\n", ans, black_num);
  }
}
