#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int> > adj_list;
vector<bool> visited1;
vector<bool> visited2;
vector<string> colour;
vector<string> input;
vector<int> cycle;
vector<int> blacked;
int cycle_len;
int cycle_root;
int next_elem(int k) {
  int i = k / m, j = k % m;
  if (input[i][j] == 'D')
    k += m;
  else if (input[i][j] == 'U')
    k -= m;
  else if (input[i][j] == 'R')
    k += 1;
  else
    k -= 1;
  return k;
}
void dfs1(int root) {
  if (visited1[root]) {
    for (int i = 0; i < cycle.size(); i++)
      if (cycle[i] == root) {
        cycle_root = root;
        cycle_len = cycle.size() - i;
        return;
      }
  }
  visited1[root] = true;
  cycle.push_back(root);
  dfs1(next_elem(root));
}
void dfs2(int root, int depth) {
  visited2[root] = true;
  blacked[depth % cycle_len] += colour[root / m][root % m] == '0';
  for (int i : adj_list[root])
    if (!visited2[i]) dfs2(i, depth + 1);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    adj_list.clear();
    visited1.clear();
    visited2.clear();
    adj_list.resize(n * m);
    visited1.resize(n * m, false);
    visited2.resize(n * m, false);
    colour.resize(n);
    input.resize(n);
    for (int i = 0; i < n; i++) cin >> colour[i];
    for (int i = 0; i < n; i++) {
      cin >> input[i];
      for (int j = 0; j < m; j++) {
        int index1 = i * m + j;
        adj_list[next_elem(index1)].push_back(index1);
      }
    }
    int black_cnt = 0;
    int total_cnt = 0;
    for (int i = 0; i < m * n; i++) {
      if (!visited2[i]) {
        cycle.clear();
        blacked.clear();
        dfs1(i);
        total_cnt += cycle_len;
        blacked.resize(cycle_len, 0);
        dfs2(cycle_root, 0);
        for (int i = 0; i < cycle_len; i++) black_cnt += (blacked[i] > 0);
      }
    }
    cout << total_cnt << " " << black_cnt << '\n';
  }
}
