#include <bits/stdc++.h>
using namespace std;
struct debug {
  template <typename T>
  debug& operator<<(const T&) {
    return *this;
  }
};
const int mod = 1e9 + 7;
const int mxn = 1e6 + 5;
int n, m;
vector<string> color, mat;
vector<vector<vector<array<int, 2>>>> inv;
vector<vector<bool>> vis, vis2;
vector<array<int, 2>> cycle;
vector<int> cnt;
int curr_size;
array<int, 2> node(int i, int j, char dir) {
  if (dir == 'U') {
    --i;
  } else if (dir == 'L') {
    --j;
  } else if (dir == 'R') {
    ++j;
  } else if (dir == 'D') {
    ++i;
  }
  return {i, j};
}
void dfs(array<int, 2> i) {
  if (vis[i[0]][i[1]]) {
    cycle.push_back(i);
    return;
  }
  vis[i[0]][i[1]] = 1;
  array<int, 2> p = node(i[0], i[1], mat[i[0]][i[1]]);
  dfs(p);
  if (cycle.back() == p && cycle[0] != i) {
    cycle.push_back(i);
  }
}
void dfs2(array<int, 2> i, int d = 0) {
  vis[i[0]][i[1]] = vis2[i[0]][i[1]] = 1;
  if (color[i[0]][i[1]] == '0') {
    ++cnt[d % curr_size];
  }
  for (auto a : inv[i[0]][i[1]]) {
    if (!vis2[a[0]][a[1]]) {
      dfs2(a, d + 1);
    }
  }
}
void solve() {
  cin >> n >> m;
  color = vector<string>(n);
  mat = vector<string>(n);
  for (int i = 0; i < n; ++i) {
    cin >> color[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> mat[i];
  }
  inv = vector<vector<vector<array<int, 2>>>>(n,
                                              vector<vector<array<int, 2>>>(m));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      array<int, 2> v = node(i, j, mat[i][j]);
      array<int, 2> u = {i, j};
      inv[v[0]][v[1]].push_back(u);
    }
  }
  vis = vis2 = vector<vector<bool>>(n, vector<bool>(m));
  int total = 0, black = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (!vis[i][j]) {
        cycle.clear();
        dfs({i, j});
        curr_size = cycle.size();
        total += curr_size;
        if (curr_size != 0) {
          cnt = vector<int>(curr_size);
          dfs2(cycle[0]);
          for (int k = 0; k < curr_size; ++k) {
            if (cnt[k] > 0) ++black;
          }
        }
      }
    }
  }
  cout << total << " " << black << "\n";
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
