#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int t, n, m, i, j, len[N];
string color[N], a[N];
vector<vector<pair<int, int>>> vis;
map<char, int> dx = {{'U', -1}, {'D', 1}, {'L', 0}, {'R', 0}};
map<char, int> dy = {{'U', 0}, {'D', 0}, {'L', -1}, {'R', 1}};
void dfs(int i, int j, int it, int d) {
  if (vis[i][j].first != -1) {
    if (vis[i][j].first == it) {
      len[it] = abs(d - vis[i][j].second);
      vis[i][j] = make_pair(it, 0);
    }
    return;
  }
  vis[i][j] = make_pair(it, d);
  int x = i + dx[a[i][j]], y = j + dy[a[i][j]];
  dfs(x, y, it, d + 1);
  int f = vis[x][y].first;
  int idx = vis[x][y].first;
  int s = (vis[x][y].second + 1) % len[idx];
  vis[i][j] = make_pair(f, s);
}
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d", &n, &m);
    vis = vector<vector<pair<int, int>>>(
        n, vector<pair<int, int>>(m, make_pair(-1, -1)));
    for (i = 0; i < n; ++i) cin >> color[i];
    for (i = 0; i < n; ++i) cin >> a[i];
    for (i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j) dfs(i, j, i * m + j, 0);
    set<pair<int, int>> st0, st1;
    for (i = 0; i < n; ++i) {
      for (j = 0; j < m; ++j) {
        st0.insert(vis[i][j]);
        if (color[i][j] == '0') st1.insert(vis[i][j]);
      }
    }
    printf("%d %d\n", (int)st0.size(), (int)st1.size());
  }
  return 0;
}
