#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 10;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
vector<int> G[N];
int vis[N];
int dfn[N];
int a[N];
int nxt[N], vis1[N];
int record[N];
int time1;
int ans1, ans2;
void bfs(int x, int len) {
  queue<pair<int, int>> q;
  q.push({0, x});
  for (int i = 0; i <= len; i++) record[i] = 0;
  while (!q.empty()) {
    int id = q.front().first;
    int f = q.front().second;
    q.pop();
    if (vis1[f]) continue;
    vis1[f] = 1;
    if (a[f] == 0) record[id % len] = 1;
    for (auto &i : G[f]) {
      q.push({id + 1, i});
    }
  }
  ans1 += len;
  for (int i = 0; i < len; i++) {
    if (record[i]) ans2++;
  }
}
void cl(int x) {
  if (vis[x]) return;
  vis[x] = 1;
  cl(nxt[x]);
  for (auto &i : G[x]) {
    cl(i);
  }
}
void dfs(int x) {
  if (dfn[x]) {
    bfs(x, time1 + 1 - dfn[x]);
  } else
    dfn[x] = ++time1, dfs(nxt[x]);
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    time1 = ans1 = ans2 = 0;
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n * m; i++) {
      vis[i] = dfn[i] = vis1[i] = 0;
      G[i].clear();
    }
    char s;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int pos = i * m + j;
        cin >> s;
        a[pos] = s - '0';
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int pos = i * m + j;
        cin >> s;
        if (s == 'L')
          nxt[pos] = pos - 1, G[pos - 1].push_back(pos);
        else if (s == 'R')
          nxt[pos] = pos + 1, G[pos + 1].push_back(pos);
        else if (s == 'U')
          nxt[pos] = pos - m, G[pos - m].push_back(pos);
        else
          nxt[pos] = pos + m, G[pos + m].push_back(pos);
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int pos = i * m + j;
        if (!vis[pos]) dfs(pos);
        cl(pos);
      }
    }
    cout << ans1 << " " << ans2 << endl;
  }
  return 0;
}
