#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int INF = 0x3f3f3f3f;
const long long INF64 = 1e18 + 1;
const int NMAX = 2000555;
const int AMAX = 205;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const char invdir[]{'L', 'U', 'R', 'D'};
const char dir[]{'R', 'D', 'L', 'U'};
vector<int> G[NMAX], Grev[NMAX], cycl[NMAX];
int viz[NMAX], on_cycl[NMAX], black[NMAX];
int col = 0;
int ncycl = 0;
void rec(int v, int start) {
  cycl[ncycl].push_back(v);
  if (G[v][0] != start) {
    rec(G[v][0], start);
  }
  return;
}
void dfs_cycl(int v) {
  viz[v] = col;
  if (viz[G[v][0]]) {
    if (viz[G[v][0]] == col) {
      ncycl++;
      rec(v, v);
      return;
    }
  } else {
    dfs_cycl(G[v][0]);
  }
}
set<int> residues;
int black_ans = 0;
int ans = 0;
void dfs_rev(int v, int res, int mod) {
  if (res < 0) {
    res += mod;
  }
  res = (res % mod);
  if (black[v] && (residues.find(res % mod) == residues.end())) {
    residues.insert(res % mod);
    black_ans++;
  }
  for (auto el : Grev[v]) {
    if (on_cycl[el]) continue;
    dfs_rev(el, res - 1, mod);
  }
}
int solve() {
  int n, m;
  ans = 0;
  black_ans = 0;
  ncycl = 0;
  cin >> n >> m;
  int N = n * m;
  col = 1;
  for (int i = 1; i <= N; ++i) {
    viz[i] = 0;
    G[i].clear();
    Grev[i].clear();
    on_cycl[i] = 0;
    cycl[i].clear();
  }
  auto node = vector<vector<int>>(n + 1, vector<int>(m + 1, 0));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      node[i][j] = (i - 1) * m + j;
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      char c;
      cin >> c;
      black[node[i][j]] = 1 - (c - '0');
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      char c;
      cin >> c;
      if (c == 'L') {
        G[node[i][j]].push_back(node[i][j - 1]);
        Grev[node[i][j - 1]].push_back(node[i][j]);
      }
      if (c == 'R') {
        G[node[i][j]].push_back(node[i][j + 1]);
        Grev[node[i][j + 1]].push_back(node[i][j]);
      }
      if (c == 'U') {
        G[node[i][j]].push_back(node[i - 1][j]);
        Grev[node[i - 1][j]].push_back(node[i][j]);
      }
      if (c == 'D') {
        G[node[i][j]].push_back(node[i + 1][j]);
        Grev[node[i + 1][j]].push_back(node[i][j]);
      }
    }
  }
  for (int i = 1; i <= N; ++i) {
    if (!viz[i]) {
      col++;
      dfs_cycl(i);
    }
  }
  for (int i = 1; i <= ncycl; ++i) {
    for (auto el : cycl[i]) {
    }
  }
  for (int i = 1; i <= ncycl; ++i) {
    for (auto el : cycl[i]) {
      on_cycl[el] = 1;
    }
    residues.clear();
    int mod = cycl[i].size();
    for (int j = 0; j < cycl[i].size(); ++j) {
      dfs_rev(cycl[i][j], j, mod);
    }
    ans += cycl[i].size();
  }
  cout << ans << " " << black_ans << "\n";
  return 0;
}
int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(0);
  std::cout << std::setprecision(16) << fixed;
  int T = 1;
  cin >> T;
  int i = 1;
  while (T--) {
    solve();
    i++;
  }
  return 0;
}
