#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    char c[n * m];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) cin >> c[i * m + j];
    vector<int> to(n * m);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        char x;
        cin >> x;
        int v = i * m + j;
        if (x == 'U') to[v] = v - m;
        if (x == 'D') to[v] = v + m;
        if (x == 'L') to[v] = v - 1;
        if (x == 'R') to[v] = v + 1;
      }
    }
    int d = 40;
    vector<vector<int>> up(n * m, vector<int>(d, 0));
    for (int i = 0; i < d; i++) {
      for (int j = 0; j < n * m; j++) {
        if (!i)
          up[j][i] = to[j];
        else
          up[j][i] = up[up[j][i - 1]][i - 1];
      }
    }
    auto go = [&](int v, int k) {
      for (int i = d - 1; i >= 0; i--) {
        if (k & (1LL << i)) {
          k ^= (1LL << i);
          v = up[v][i];
        }
      }
      return v;
    };
    int all = 0, black = 0;
    vector<bool> used(n * m);
    for (int i = 0; i < n * m; i++) {
      if (c[i] == '0') {
        int v = go(i, 2 * n * m);
        if (!used[v]) {
          used[v] = 1;
          black++;
          all++;
        }
      }
    }
    for (int i = 0; i < n * m; i++) {
      if (c[i] == '1') {
        int v = go(i, 2 * n * m);
        if (!used[v]) {
          used[v] = 1;
          all++;
        }
      }
    }
    cout << all << " " << black << "\n";
  }
}
