#include <bits/stdc++.h>
using namespace std;
pair<int, int> fa[1000010];
unordered_map<char, int> P;
int v[1000010], mp[1000010], vis[1000010], Vis[1000010], dis[1000010],
    rk[1000010];
bool oc[1000010];
char s[1000010];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int cnt, n, m, Cnt, now, ans, Ans;
vector<pair<int, int> > p[1000010];
int as[1000010];
void dfs2(int x, int y) {
  p[Cnt].push_back({x, y});
  Vis[((x - 1) * (m) + y)] = Cnt, dis[((x - 1) * (m) + y)] = 0,
                       fa[((x - 1) * (m) + y)] = {x, y},
                       rk[((x - 1) * (m) + y)] = now++;
  int xx = x + dx[mp[((x - 1) * (m) + y)]],
      yy = y + dy[mp[((x - 1) * (m) + y)]];
  if (Vis[((xx - 1) * (m) + yy)] == Cnt) return;
  dfs2(xx, yy);
}
void dfs1(int x, int y) {
  vis[((x - 1) * (m) + y)] = cnt;
  int xx = x + dx[mp[((x - 1) * (m) + y)]],
      yy = y + dy[mp[((x - 1) * (m) + y)]];
  if (vis[((xx - 1) * (m) + yy)] == 0)
    dfs1(xx, yy);
  else if (vis[((xx - 1) * (m) + yy)] != cnt)
    return;
  else {
    ++Cnt, now = 0, dfs2(xx, yy);
    return;
  }
}
pair<int, int> find(int x, int y) {
  auto v = fa[((x - 1) * (m) + y)];
  int fx = v.first, fy = v.second;
  if (Vis[((fx - 1) * (m) + fy)]) return v;
  fa[((x - 1) * (m) + y)] = find(fx, fy);
  dis[((x - 1) * (m) + y)] += dis[((fx - 1) * (m) + fy)];
  return fa[((x - 1) * (m) + y)];
}
inline void solve() {
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s + 1);
    for (int j = 1; j <= m; ++j) v[((i - 1) * (m) + j)] = (s[j] - 48) ^ 1;
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s + 1);
    for (int j = 1; j <= m; ++j) {
      mp[((i - 1) * (m) + j)] = P[s[j]];
      int x = i + dx[mp[((i - 1) * (m) + j)]],
          y = j + dy[mp[((i - 1) * (m) + j)]];
      fa[((i - 1) * (m) + j)] = {x, y};
    }
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (!vis[((i - 1) * (m) + j)]) ++cnt, dfs1(i, j);
  ans = Ans = 0;
  for (int i = 1; i <= Cnt; ++i) {
    ans += p[i].size();
    for (auto w : p[i]) as[i] += v[((w.first - 1) * (m) + w.second)];
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (!Vis[((i - 1) * (m) + j)] && v[((i - 1) * (m) + j)]) {
        auto o = find(i, j);
        int x = o.first, y = o.second, siz = p[Vis[((x - 1) * (m) + y)]].size();
        int d = dis[((i - 1) * (m) + j)] % siz,
            id = rk[((x - 1) * (m) + y)] + siz;
        auto O = p[Vis[((x - 1) * (m) + y)]][(id - d) % siz];
        if (oc[((O.first - 1) * (m) + O.second)]) continue;
        as[Vis[((x - 1) * (m) + y)]] += v[((O.first - 1) * (m) + O.second)] ^ 1;
        oc[((O.first - 1) * (m) + O.second)] = 1;
      }
  for (int i = 1; i <= Cnt; ++i) Ans += as[i];
  printf("%d %d\n", ans, Ans);
}
inline void init() {
  scanf("%d%d", &n, &m), cnt = Cnt = 0;
  for (int i = 1, j = n * m; i <= j; ++i) {
    p[i].clear(), as[i] = 0;
    vis[i] = Vis[i] = rk[i] = oc[i] = 0;
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) dis[((i - 1) * (m) + j)] = 1;
}
int main() {
  int T = 1;
  P['U'] = 0, P['R'] = 1, P['D'] = 2, P['L'] = 3;
  scanf("%d", &T);
  while (T--) init(), solve();
}
