#include <bits/stdc++.h>
using namespace std;
const int SZ = 1000003;
vector<int> col(SZ);
vector<vector<int>> g(SZ);
vector<vector<int>> gT(SZ);
vector<int> roots, cycsz;
vector<int> vis;
vector<int> when;
int curtime = 0, start = 0;
void reset1(int N) {
  roots.clear();
  cycsz.clear();
  vis.resize(N);
  when.resize(N);
  for (int i = 0; i < N; i++) {
    vis[i] = 0;
    when[i] = 0;
  }
  curtime = start = 0;
}
void dfs1(int u) {
  vis[u] = 1;
  when[u] = curtime++;
  for (int v : g[u]) {
    if (!vis[v])
      dfs1(v);
    else {
      if (start <= when[v]) {
        roots.push_back(v);
        cycsz.push_back(when[u] + 1 - when[v]);
      }
    }
  }
}
vector<int> freq;
int cnt = 0, sz = 0;
void dfs2(int u, int dist) {
  vis[u] = 1;
  if (dist >= sz) dist -= sz;
  if (!col[u]) {
    if (!freq[dist]) ++cnt;
    ++freq[dist];
  }
  for (int v : gT[u]) {
    if (!vis[v]) dfs2(v, dist + 1);
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<char>> dir(n, vector<char>(m));
  int N = n * m;
  col.resize(N);
  for (int u = 0; u < N; u++) {
    g[u].clear();
    gT[u].clear();
  }
  int it = 0;
  for (int i = 0; i < n; i++) {
    string line;
    cin >> line;
    for (int j = 0; j < m; j++) {
      col[it++] = line[j] - '0';
    }
  }
  for (int i = 0; i < n; i++) {
    string line;
    cin >> line;
    for (int j = 0; j < m; j++) {
      dir[i][j] = line[j];
    }
  }
  int u = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int x = i, y = j;
      if (dir[i][j] == 'L') --y;
      if (dir[i][j] == 'R') ++y;
      if (dir[i][j] == 'U') --x;
      if (dir[i][j] == 'D') ++x;
      int v = m * x + y;
      g[u].push_back(v);
      gT[v].push_back(u);
      ++u;
    }
  }
  int ans = 0;
  reset1(N);
  for (int u = 0; u < N; u++) {
    if (!vis[u]) {
      dfs1(u);
      start = curtime;
    }
  }
  for (int val : cycsz) ans += val;
  int bcnt = 0;
  assert((int)cycsz.size() == (int)roots.size());
  for (int i = 0; i < N; i++) vis[i] = 0;
  int idx = 0;
  for (int u : roots) {
    cnt = 0;
    sz = cycsz[idx++];
    freq.resize(sz);
    for (int it = 0; it < sz; ++it) freq[it] = 0;
    dfs2(u, 0);
    bcnt += cnt;
  }
  cout << ans << " " << bcnt << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tc;
  cin >> tc;
  while (tc--) solve();
}
