#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> c(n * m);
  vector<bool> used(n * m), used2(n * m);
  vector<vector<int>> g(n * m);
  vector<int> nxt(n * m);
  map<pair<int, int>, int> kek;
  int tot = 0;
  auto get = [&](int i, int j) {
    auto pp = make_pair(i, j);
    if (kek.count(pp)) {
      return kek[pp];
    }
    return kek[pp] = tot++;
  };
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      char ch;
      cin >> ch;
      c[get(i, j)] = ch - '0';
      c[get(i, j)] ^= 1;
    }
  }
  for (int i = 0; i < n; ++i) {
    string d;
    cin >> d;
    for (int j = 0; j < m; ++j) {
      int ni = i, nj = j;
      if (d[j] == 'R') {
        ++nj;
      } else if (d[j] == 'L') {
        --nj;
      } else if (d[j] == 'D') {
        ++ni;
      } else {
        --ni;
      }
      nxt[get(i, j)] = get(ni, nj);
      g[get(ni, nj)].push_back(get(i, j));
    }
  }
  int ans = 0, ansb = 0;
  for (int i = 0; i < n * m; ++i) {
    if (used[i]) {
      continue;
    }
    int v = i;
    while (!used2[v]) {
      used2[v] = true;
      v = nxt[v];
    }
    int u = v;
    vector<int> cycle;
    do {
      cycle.push_back(v);
      v = nxt[v];
      assert(cycle.size() <= n * m);
    } while (u != v);
    int sz = cycle.size();
    vector<int> cc(sz);
    ans += sz;
    function<void(int, int, int)> dfs = [&](int v, int d, int i) {
      used[v] = true;
      if (c[v]) {
        int j = (i - d + 1ll * d * sz) % sz;
        cc[j] = 1;
      }
      for (int to : g[v]) {
        if (used[to]) {
          continue;
        }
        dfs(to, d + 1, i);
      }
    };
    for (int j = 0; j < sz; ++j) {
      dfs(cycle[j], 0, j);
    }
    for (int x : cc) {
      ansb += x;
    }
  }
  cout << ans << ' ' << ansb;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
    cout << '\n';
  }
  return 0;
}
