#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6 + 2;
const int nmlog = 22;
int nxt[MAX][23];
int color[MAX];
int dp[MAX];
int dp_new[MAX];
int robots[MAX], black[MAX];
void solve() {
  int n, m;
  vector<char> a;
  cin >> n >> m;
  a.resize(n * m);
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      color[i * m + j] = (s[j] == '0');
    }
  }
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      int idx;
      if (s[j] == 'U') idx = (i - 1) * m + j;
      if (s[j] == 'D') idx = (i + 1) * m + j;
      if (s[j] == 'L') idx = i * m + j - 1;
      if (s[j] == 'R') idx = i * m + j + 1;
      dp[i * m + j] = idx;
    }
  }
  int sz = n * m;
  for (int deg = 1; deg <= nmlog; deg++) {
    for (int i = 0; i < sz; i++) {
      dp_new[i] = dp[dp[i]];
    }
    for (int i = 0; i < sz; i++) {
      dp[i] = dp_new[i];
    }
  }
  fill(robots, robots + sz, 0);
  fill(black, black + sz, 0);
  for (int i = 0; i < sz; i++) {
    robots[dp[i]]++;
    black[dp[i]] += color[i];
  }
  int max_robots = 0;
  int max_black = 0;
  for (int i = 0; i < sz; i++) {
    if (robots[i]) {
      max_robots++;
    }
    if (black[i]) {
      max_black++;
    }
  }
  cout << max_robots << " " << max_black << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) solve();
}
