#include <bits/stdc++.h>
using namespace std;
stack<long long> st;
vector<long long> adj[1000006];
vector<long long> radj[1000006];
long long col[1000006];
long long parent[1000006];
long long siz[1000006];
long long vis[1000006];
long long val[1000006];
void make_set(int v) {
  parent[v] = v;
  siz[v] = 1;
}
int find_set(int v) {
  if (v == parent[v]) return v;
  return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (siz[a] < siz[b]) swap(a, b);
    parent[b] = a;
    siz[a] += siz[b];
  }
}
void dfs(long long u) {
  vis[u] = 1;
  for (auto i : adj[u])
    if (vis[i] == 0) dfs(i);
  st.push(u);
}
void dfs1(long long u, long long c) {
  vis[u] = 1;
  for (auto i : radj[u])
    if (vis[i] == 0) union_sets(u, i), dfs1(i, ++c);
}
long long nex(long long x, long long d) {
  if (x == 1) return d;
  return x - 1;
}
void dfs2(long long u, long long c, long long s) {
  vis[u] = 1;
  val[c] = val[c] | col[u];
  for (auto i : radj[u])
    if (vis[i] == 0) dfs2(i, nex(c, s), s);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    for (long long i = 0; i < n * m; i++)
      make_set(i), val[i] = 0, adj[i].clear(), radj[i].clear(), col[i] = 0,
                   vis[i] = 0;
    string s[n];
    string c[n];
    for (long long i = 0; i < n; i++) cin >> c[i];
    for (long long i = 0; i < n; i++) {
      cin >> s[i];
      for (long long j = 0; j < s[i].length(); j++) {
        long long u = i * m + j;
        col[u] = 1 - (c[i][j] - '0');
        if (s[i][j] == 'U')
          adj[u].push_back((i - 1) * m + j), radj[(i - 1) * m + j].push_back(u);
        if (s[i][j] == 'D')
          adj[u].push_back((i + 1) * m + j), radj[(i + 1) * m + j].push_back(u);
        if (s[i][j] == 'L')
          adj[u].push_back(i * m + j - 1), radj[i * m + j - 1].push_back(u);
        if (s[i][j] == 'R')
          adj[u].push_back(i * m + j + 1), radj[i * m + j + 1].push_back(u);
      }
    }
    for (long long i = 0; i < n * m; i++)
      if (vis[i] == 0) dfs(i);
    for (long long i = 0; i < n * m; i++) vis[i] = 0;
    while (!st.empty()) {
      long long u = st.top();
      st.pop();
      if (vis[u] == 0) dfs1(u, 1);
    }
    for (long long i = 0; i < n * m; i++) vis[i] = 0;
    long long ans1 = 0, ans2 = 0;
    for (long long i = 0; i < n * m; i++) {
      if (vis[i]) continue;
      long long p = find_set(i);
      if (siz[p] > 1) {
        ans1 += siz[p];
        for (long long j = 0; j < siz[p] + 1; j++) val[j] = 0;
        dfs2(i, 1, siz[p]);
        for (long long j = 0; j < siz[p] + 1; j++) ans2 += val[j];
      }
    }
    cout << ans1 << " " << ans2 << "\n";
  }
}
