#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
using namespace std;
const int N = 2e6 + 1000, OO = 1e5 + 7, T = 13, M = 998244353, P = 6151,
          SQ = 360, lg = 34;
vector<int> v, cnt[N];
set<int> s[N];
int nxt[N], st = 0, koj[N], dis[N], cmp[N], wh[N], en[N], ht = 0, ans = 0;
bool mark[N], is[N];
void go(int x) {
  mark[x] = true;
  en[x] = ht;
  if (!mark[nxt[x]]) {
    go(nxt[x]);
    return;
  }
  if (en[nxt[x]] == en[x]) v.push_back(nxt[x]);
  return;
}
void dfs(int x) {
  mark[x] = true;
  if (!mark[nxt[x]]) dfs(nxt[x]);
  if (cmp[x] != 0) {
    wh[x] = x;
    dis[x] = 0;
    return;
  }
  wh[x] = wh[nxt[x]];
  dis[x] = dis[nxt[x]] + 1;
  return;
}
void go2(int x) {
  ans++;
  cmp[x] = st;
  mark[x] = true;
  koj[x] = (int)cnt[st].size();
  cnt[st].push_back(x);
  if (!mark[nxt[x]]) go2(nxt[x]);
}
void solve() {
  int n, m;
  cin >> n >> m;
  fill(mark, mark + ((n - 1) * m + m) + 10, false);
  fill(is, is + ((n - 1) * m + m) + 10, false);
  fill(cmp, cmp + ((n - 1) * m + m) + 10, 0);
  fill(en, en + ((n - 1) * m + m) + 10, 0);
  fill(wh, wh + ((n - 1) * m + m) + 10, 0);
  fill(dis, dis + ((n - 1) * m + m) + 10, 0);
  fill(koj, koj + ((n - 1) * m + m) + 10, 0);
  v.clear();
  ht = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char x;
      cin >> x;
      if (x == '0') is[((i - 1) * m + j)] = true;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      if (c == 'R') nxt[((i - 1) * m + j)] = ((i - 1) * m + j + 1);
      if (c == 'L') nxt[((i - 1) * m + j)] = ((i - 1) * m + j - 1);
      if (c == 'U') nxt[((i - 1) * m + j)] = ((i - 1 - 1) * m + j);
      if (c == 'D') nxt[((i - 1) * m + j)] = ((i + 1 - 1) * m + j);
    }
  }
  st = 1, ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (!mark[((i - 1) * m + j)]) go(((i - 1) * m + j)), ht++;
  fill(mark, mark + ((n - 1) * m + m) + 10, false);
  for (auto u : v) {
    go2(u), st++;
  }
  fill(mark, mark + ((n - 1) * m + m) + 10, false);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (!mark[((i - 1) * m + j)]) dfs(((i - 1) * m + j));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (!is[((i - 1) * m + j)]) continue;
      int c = cmp[wh[((i - 1) * m + j)]];
      int q = (int)cnt[c].size();
      s[c].insert(
          cnt[c]
             [((koj[wh[((i - 1) * m + j)]] - dis[((i - 1) * m + j)]) % q + q) %
              q]);
    }
  }
  int mx = 0;
  for (int i = 1; i < st; i++) {
    mx += (int)s[i].size();
    s[i].clear();
    cnt[i].clear();
  }
  cout << ans << " " << mx << endl;
}
int32_t main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
