#include <bits/stdc++.h>
using namespace std;
const int LOG = 21;
map<char, int> dy = {{'L', -1}, {'R', 1}, {'U', 0}, {'D', 0}};
map<char, int> dx = {{'L', 0}, {'R', 0}, {'U', -1}, {'D', 1}};
int n, m;
inline int get_num(int x, int y) { return x * m + y; }
void solve() {
  cin >> n >> m;
  vector<string> color(n);
  for (auto &u : color) cin >> u;
  vector<string> fild(n);
  for (auto &u : fild) cin >> u;
  vector<int> go(n * m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      go[get_num(i, j)] = get_num(i + dx[fild[i][j]], j + dy[fild[i][j]]);
    }
  }
  vector<int> new_go(n * m);
  for (int k = 0; k < LOG; k++) {
    for (int i = 0; i < n * m; i++) {
      new_go[i] = go[go[i]];
    }
    go = new_go;
  }
  vector<char> is_black(n * m, 0), good(n * m, 0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      good[go[get_num(i, j)]] = 1;
      if (color[i][j] == '0') is_black[go[get_num(i, j)]] = 1;
    }
  }
  cout << accumulate(good.begin(), good.end(), 0) << ' '
       << accumulate(is_black.begin(), is_black.end(), 0) << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
}
