#include <bits/stdc++.h>
using namespace std;
char s[1000002];
char v1[1000002], v2[1000002];
int cnt[1000002];
vector<int> g[1000002], g2[1000002];
stack<int> st;
bool vis[1000002];
void dfs1(int u) {
  vis[u] = 1;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (vis[v]) continue;
    dfs1(v);
  }
  st.push(u);
}
vector<int> co;
void dfs2(int u) {
  vis[u] = 1;
  co.emplace_back(u);
  for (int i = 0; i < g2[u].size(); i++) {
    int v = g2[u][i];
    if (vis[v]) continue;
    dfs2(v);
  }
}
int b = 0, sum = 0;
void dfs3(int u) {
  vis[u] = 1;
  sum = max(sum, cnt[u]);
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (vis[v]) continue;
    dfs3(v);
  }
  for (int i = 0; i < g2[u].size(); i++) {
    int v = g2[u][i];
    if (vis[v]) continue;
    dfs3(v);
  }
}
bool S[1000002];
void dfs4(int u, int d, int sz) {
  vis[u] = 0;
  if (v1[u] == 1) S[d] = 1;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (!vis[v]) continue;
    dfs4(v, (d + 1) % sz, sz);
  }
  for (int i = 0; i < g2[u].size(); i++) {
    int v = g2[u][i];
    if (!vis[v]) continue;
    dfs4(v, (d - 1 + sz) % sz, sz);
  }
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) {
      scanf("%s", s);
      for (int j = 0; j < m; j++) {
        v1[i * m + j] = s[j] - '0';
        v1[i * m + j] = 1 - v1[i * m + j];
      }
    }
    for (int i = 0; i < n; i++) {
      scanf("%s", s);
      for (int j = 0; j < m; j++) {
        v2[i * m + j] = s[j];
        vis[i * m + j] = 0;
        g[i * m + j].clear();
        g2[i * m + j].clear();
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (v2[i * m + j] == 'U') {
          g[i * m + j].emplace_back((i - 1) * m + j);
          g2[(i - 1) * m + j].emplace_back(i * m + j);
        } else if (v2[i * m + j] == 'D') {
          g[i * m + j].emplace_back((i + 1) * m + j);
          g2[(i + 1) * m + j].emplace_back(i * m + j);
        } else if (v2[i * m + j] == 'R') {
          g[i * m + j].emplace_back(i * m + j + 1);
          g2[i * m + j + 1].emplace_back(i * m + j);
        } else {
          g[i * m + j].emplace_back(i * m + j - 1);
          g2[i * m + j - 1].emplace_back(i * m + j);
        }
      }
    }
    while (!st.empty()) st.pop();
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) vis[i * m + j] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (vis[i * m + j]) continue;
        dfs1(i * m + j);
      }
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) vis[i * m + j] = 0;
    while (!st.empty()) {
      int u = st.top();
      st.pop();
      if (vis[u]) continue;
      co.clear();
      dfs2(u);
      for (int i = 0; i < co.size(); i++) {
        int u = co[i];
        cnt[u] = (int)co.size();
      }
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) vis[i * m + j] = 0;
    int ans1 = 0, ans2 = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (vis[i * m + j]) continue;
        sum = 0, b = 0;
        dfs3(i * m + j);
        ans1 += sum;
        for (int k = 0; k <= sum; k++) S[k] = 0;
        dfs4(i * m + j, 0, sum);
        for (int k = 0; k <= sum; k++) b += S[k];
        dfs3(i * m + j);
        ans2 += b;
      }
    }
    printf("%d %d\n", ans1, ans2);
  }
}
