#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  std::cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
template <typename T, typename U>
static inline void amin(T& x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
static inline void amax(T& x, U y) {
  if (x < y) x = y;
}
int max(int a, int b) { return (a > b) ? a : b; }
int min(int a, int b) { return (a < b) ? a : b; }
vector<string> c, s;
const int N = 1e6 + 1;
bool vis[N];
bool vis2[N];
int n, m;
const int LOG = 21;
int to[N][LOG];
void dfs(int x, int y) {
  int nx = x, ny = y;
  vis[x * m + y] = 1;
  if (s[x][y] == 'L')
    ny--;
  else if (s[x][y] == 'R')
    ny++;
  else if (s[x][y] == 'U')
    nx--;
  else if (s[x][y] == 'D')
    nx++;
  if (!vis[nx * m + ny]) dfs(nx, ny);
  to[x * m + y][0] = nx * m + ny;
}
int solve() {
  ;
  cin >> n >> m;
  int nm = n * m;
  c.resize(n);
  s.resize(n);
  for (int i = 0; i < n; ++i) cin >> c[i];
  for (int i = 0; i < n; ++i) cin >> s[i];
  memset(vis, 0, sizeof(bool) * nm);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!vis[i * m + j]) dfs(i, j);
    }
  }
  for (int deg = 1; deg < LOG; deg++)
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int v = i * m + j;
        to[v][deg] = to[to[v][deg - 1]][deg - 1];
      }
    }
  memset(vis, 0, sizeof(bool) * nm);
  memset(vis2, 0, sizeof(bool) * nm);
  int a1 = 0, a2 = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int v = i * m + j;
      for (int deg = LOG - 1; deg >= 0; deg--) {
        if (nm & (1 << deg)) {
          v = to[v][deg];
        }
      }
      vis2[v] |= (c[i][j] == '0');
      vis[v] = 1;
    }
  }
  for (int i = 0; i < nm; i++) {
    a1 += vis[i];
    a2 += vis2[i];
  }
  cout << a1 << " " << a2 << '\n';
  return 0;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
