#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e18;
const long long mod = (long long)1e9 + 7;
const double eps = (double)1e-9;
const double pi = acos(-1.0);
const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};
const int N = 1000500;
char s[N];
bool oncyc[N];
vector<int> cycle, gr[N];
int n, m, timer, u[N], p[N], col[N], nxt[N], was[N];
inline int getid(int x, int y) { return (x - 1) * m + y; }
void dfs(int v) {
  u[v] = 1;
  if (!u[nxt[v]]) {
    p[nxt[v]] = v;
    dfs(nxt[v]);
  } else if (u[nxt[v]] == 1) {
    cycle.clear();
    int w = v;
    while (1) {
      cycle.push_back(w);
      oncyc[w] = 1;
      if (w == nxt[v]) break;
      w = p[w];
    }
  }
  u[v] = 2;
}
void dfs1(int v, int dist, int add, int len) {
  if (!col[v]) was[(dist + add) % len] = timer;
  for (auto to : gr[v])
    if (!oncyc[to]) dfs1(to, dist + 1, add, len);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++) {
      scanf("%s", s + 1);
      for (int j = 1; j <= m; j++) {
        col[getid(i, j)] = s[j] - '0';
      }
    }
    for (int i = 1; i <= n; i++) {
      scanf("%s", s + 1);
      for (int j = 1; j <= m; j++) {
        int from = getid(i, j);
        int to;
        if (s[j] == 'U')
          to = getid(i - 1, j);
        else if (s[j] == 'D')
          to = getid(i + 1, j);
        else if (s[j] == 'R')
          to = getid(i, j + 1);
        else
          to = getid(i, j - 1);
        nxt[from] = to;
        gr[to].push_back(from);
      }
    }
    int tot = n * m, ans = 0, mx = 0;
    for (int i = 1; i <= tot; i++) {
      if (u[i]) continue;
      dfs(i);
      if (cycle.empty()) continue;
      int add = 0, len = (int)cycle.size();
      ans += len;
      timer++;
      for (auto j : cycle) {
        dfs1(j, 0, add, len);
        add++;
        if (add >= len) add -= len;
      }
      for (int j = 0; j < (int)cycle.size(); j++) {
        mx += (was[j] == timer);
      }
      cycle.clear();
    }
    for (int i = 1; i <= tot; i++) {
      gr[i].clear();
      u[i] = oncyc[i] = 0;
    }
    printf("%d %d\n", ans, mx);
  }
  return 0;
}
