#include <bits/stdc++.h>
using namespace std;
int n, m, s;
void solve() {
  cin >> n >> m;
  s = n * m;
  vector<string> c(n), d(n);
  for (int i = 0; i < n; i++) cin >> c[i];
  for (int i = 0; i < n; i++) cin >> d[i];
  vector<vector<int>> nxt(s, vector<int>(30));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int x = i * m + j, u = i, v = j;
      if (d[i][j] == 'U')
        u--;
      else if (d[i][j] == 'R')
        v++;
      else if (d[i][j] == 'D')
        u++;
      else
        v--;
      int y = u * m + v;
      nxt[x][0] = y;
    }
  }
  for (int j = 1; j < 30; j++)
    for (int i = 0; i < s; i++) nxt[i][j] = nxt[nxt[i][j - 1]][j - 1];
  int res[] = {0, 0};
  vector<int> mark(s, 0), color(s, 0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int x = i * m + j, y = nxt[x][29];
      if (!mark[y]) {
        mark[y] = 1, color[y] = 1 - (c[i][j] - '0');
        res[0] += 1;
        res[1] += color[y];
      } else if (!color[y]) {
        if (c[i][j] == '0') ++res[1], color[y] = 1;
      }
    }
  }
  cout << res[0] << " " << res[1] << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
