#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e6 + 100;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
long long q, t, n, m, h[maxn], a[maxn], c[maxn], d[maxn], e, s, r;
vector<long long> g, b[maxn];
bool f[maxn], vis[maxn], val[maxn];
char ch;
void dfs(long long v, long long h) {
  if (vis[v]) {
    if (h != d[v]) e = abs(h - d[v]);
    return;
  }
  long long u;
  vis[v] = 1;
  d[v] = h;
  g.push_back(v);
  dfs(a[v], h + 1);
  for (long long i = 0; i < b[v].size(); i++) {
    u = b[v][i];
    dfs(u, h - 1);
  }
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> q;
  while (q--) {
    cin >> n >> m;
    s = r = 0;
    fill(vis, vis + n * m, 0);
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < m; j++) {
        cin >> ch;
        if (ch == '1')
          f[i * m + j] = 1;
        else
          f[i * m + j] = 0;
        b[i * m + j].clear();
      }
    }
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < m; j++) {
        cin >> ch;
        if (ch == 'R')
          a[i * m + j] = i * m + j + 1, b[i * m + j + 1].push_back(i * m + j);
        if (ch == 'L')
          a[i * m + j] = i * m + j - 1, b[i * m + j - 1].push_back(i * m + j);
        if (ch == 'U')
          a[i * m + j] = i * m + j - m, b[i * m + j - m].push_back(i * m + j);
        if (ch == 'D')
          a[i * m + j] = i * m + j + m, b[i * m + j + m].push_back(i * m + j);
      }
    }
    for (long long i = 0; i < n * m; i++) {
      if (!vis[i]) {
        g.clear();
        dfs(i, 0);
        for (long long j = 0; j < g.size(); j++) {
          if (f[g[j]]) continue;
          if (!val[(d[g[j]] % e + e) % e]) r++;
          val[(d[g[j]] % e + e) % e] = 1;
        }
        s += e;
        for (long long j = 0; j < g.size(); j++) {
          val[(d[g[j]] % e + e) % e] = 0;
        }
      }
    }
    cout << s << " " << r << '\n';
  }
  return 0;
}
