#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e6 + 5;
const long long maxm = 1e3 + 5;
int ans2, ans, cys, dfsclock, n, m, t;
char ch;
int fir[maxn], dis[maxn], siz[maxn];
int cy[maxn], dfn[maxn], low[maxn], go[maxn];
bool mod[maxn], qaq[maxn], ins[maxn], col[maxn];
stack<int> s;
vector<int> Adj[maxn];
inline int cal(int x, int y) { return (x - 1) * m + y; }
void Tarjan(long long p) {
  dfn[p] = low[p] = ++dfsclock;
  s.push(p);
  ins[p] = 1;
  long long t = go[p];
  if (!dfn[t]) {
    Tarjan(t);
    low[p] = min(low[p], low[t]);
  } else {
    if (ins[t]) {
      low[p] = min(low[p], dfn[t]);
    }
  }
  if (low[p] == dfn[p]) {
    if (s.top() == p) {
      ins[p] = 0;
      s.pop();
      return;
    }
    cys++;
    fir[cys] = p;
    while (s.top() != p) {
      cy[s.top()] = cys;
      ins[s.top()] = 0;
      ans++;
      siz[cys]++;
      s.pop();
    }
    cy[p] = cys;
    ins[p] = 0;
    s.pop();
    ans++;
    siz[cys]++;
  }
  return;
}
int main() {
  cin >> t;
  while (t--) {
    cin >> n >> m;
    ans = ans2 = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> ch;
        col[cal(i, j)] = ch - '0';
      }
    }
    for (int i = 1; i <= n * m; i++) {
      cy[i] = 0;
      int len = Adj[i].size();
      for (int j = 0; j < len; j++) {
        Adj[i].pop_back();
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> ch;
        if (ch == 'L') {
          go[cal(i, j)] = cal(i, j - 1);
          Adj[cal(i, j - 1)].push_back(cal(i, j));
        } else if (ch == 'R') {
          go[cal(i, j)] = cal(i, j + 1);
          Adj[cal(i, j + 1)].push_back(cal(i, j));
        } else if (ch == 'U') {
          go[cal(i, j)] = cal(i - 1, j);
          Adj[cal(i - 1, j)].push_back(cal(i, j));
        } else if (ch == 'D') {
          go[cal(i, j)] = cal(i + 1, j);
          Adj[cal(i + 1, j)].push_back(cal(i, j));
        }
      }
    }
    cys = dfsclock = 0;
    for (int i = 1; i <= n * m; i++) {
      dfn[i] = 0;
      siz[i] = 0;
    }
    for (int i = 1; i <= n * m; i++) {
      if (!dfn[i]) {
        Tarjan(i);
      }
    }
    for (int i = 1; i <= cys; i++) {
      long long CycleID = i;
      for (int j = 0; j < siz[CycleID]; j++) {
        mod[j] = 0;
      }
      int id = 0, op = fir[i];
      for (int j = 0; j < siz[CycleID]; j++) {
        queue<int> q;
        q.push(op);
        dis[op] = 0;
        while (!q.empty()) {
          int fr = q.front();
          q.pop();
          if (!col[fr]) {
            mod[((id - dis[fr]) % siz[CycleID] + siz[CycleID]) % siz[CycleID]] =
                1;
          }
          for (int k = 0; k < Adj[fr].size(); k++) {
            int R = Adj[fr][k];
            if (cy[R]) {
              continue;
            }
            q.push(R);
            dis[R] = dis[fr] + 1;
          }
        }
        op = go[op];
        id++;
      }
      for (int j = 0; j < siz[CycleID]; j++) {
        if (mod[j]) {
          ans2++;
        }
      }
    }
    cout << ans << " " << ans2 << endl;
  }
  return 0;
}
