#include <bits/stdc++.h>
using namespace std;
vector<string> c, s;
vector<vector<int>> v, g, cnt;
vector<int> sz;
int cur;
pair<int, int> f(int y, int x, int d) {
  if (g[y][x]) return {g[y][x], v[y][x]};
  if (v[y][x]) {
    sz.push_back(d - v[y][x]);
    v[y][x] = 1;
    return {cur++, 1};
  }
  v[y][x] = d;
  pair<int, int> r;
  switch (s[y][x]) {
    case 'U':
      r = f(y - 1, x, d + 1);
      break;
    case 'D':
      r = f(y + 1, x, d + 1);
      break;
    case 'L':
      r = f(y, x - 1, d + 1);
      break;
    case 'R':
      r = f(y, x + 1, d + 1);
      break;
  }
  g[y][x] = r.first;
  v[y][x] = r.second + 1;
  return {r.first, r.second + 1};
}
void solve() {
  int i, j;
  int n, m;
  cin >> n >> m;
  c.resize(n);
  s.resize(n);
  g = v = vector<vector<int>>(n, vector<int>(m, 0));
  cur = 1;
  sz.resize(1);
  for (i = 0; i < n; i++) cin >> c[i];
  for (i = 0; i < n; i++) cin >> s[i];
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
      if (!v[i][j]) f(i, j, 1);
  cnt = vector<vector<int>>(cur + 1);
  int mx = 0, mxb = 0;
  for (i = 1; i < cur; i++) {
    cnt[i] = vector<int>(sz[i] + 1, 0);
    mx += sz[i];
  }
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
      if (c[i][j] == '0' && cnt[g[i][j]][v[i][j] % sz[g[i][j]]]++ == 0) mxb++;
  cout << mx << ' ' << mxb << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
}
