#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int b[N], col[N], fix[N], ind[N], t, n, m, u, M, a[N];
vector<int> V[N], cycle[N];
void dfs(int u, int p, int h, int d, int U) {
  if (col[u]) {
    b[cycle[d][(ind[U] - h % cycle[d].size() + cycle[d].size()) %
               cycle[d].size()]] = 1;
  }
  for (int i = 0; i < V[u].size(); i++) {
    if (V[u][i] == p || fix[V[u][i]] > M) continue;
    dfs(V[u][i], u, h + 1, d, U);
  }
}
int main() {
  cin >> t;
  while (t--) {
    cin >> n >> m;
    int ans = 0, bl = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        int ind = (i - 1) * m + j;
        fix[ind] = b[ind] = 0;
        char b;
        cin >> b;
        col[ind] = 1 - b + '0';
        V[ind].clear();
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        int ind = (i - 1) * m + j;
        char b;
        cin >> b;
        if (b == 'L')
          a[ind] = (ind - 1), V[ind - 1].push_back(ind);
        else if (b == 'R')
          a[ind] = (ind + 1), V[ind + 1].push_back(ind);
        else if (b == 'U')
          a[ind] = (ind - m), V[ind - m].push_back(ind);
        else
          a[ind] = (ind + m), V[ind + m].push_back(ind);
      }
    }
    M = n * m;
    int cnt = 0;
    for (int i = 1; i <= n * m; i++) {
      if (!V[i].size()) {
        u = i;
        while (!fix[u]) {
          fix[u] = i;
          u = a[u];
        }
        if (i == fix[u]) {
          int idx = 0;
          cnt++;
          while (fix[u] != M + 2) {
            fix[u] = M + 2;
            ind[u] = idx;
            idx++;
            cycle[cnt].push_back(u);
            u = a[u];
          }
        }
      }
    }
    for (int i = 1; i <= cnt; i++) {
      for (int j = 0; j < cycle[i].size(); j++) {
        dfs(cycle[i][j], 0, 0, i, cycle[i][j]);
      }
      for (int j = 0; j < cycle[i].size(); j++) {
        if (b[cycle[i][j]]) bl++;
      }
      ans += cycle[i].size();
      cycle[i].clear();
    }
    for (int i = 1; i <= M; i++) {
      if (!fix[i]) {
        u = i;
        while (!fix[u]) {
          fix[u] = i;
          if (col[u]) bl++;
          ans++;
          u = a[u];
        }
      }
    }
    cout << ans << " " << bl << endl;
  }
}
