#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6;
const int INF = 0x3f3f3f3f, MOD = 1000000007;
const float EPS = 1e-6;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
int nxt[MAXN], vst[MAXN], clr[MAXN], cnt[MAXN];
vector<int> lst[MAXN];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int cans = 0, lans = 0;
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = (0); i < (n); i += 1) {
      for (int j = (0); j < (m); j += 1) {
        vst[i * m + j] = 0;
        lst[i * m + j].clear();
        char ch;
        do scanf("%c", &ch);
        while (ch == '\n');
        clr[i * m + j] = ch - '0';
      }
    }
    for (int i = (0); i < (n); i += 1) {
      for (int j = (0); j < (m); j += 1) {
        char ch;
        do scanf("%c", &ch);
        while (ch == '\n');
        int ri = i, rj = j;
        if (ch == 'U') ri--;
        if (ch == 'D') ri++;
        if (ch == 'L') rj--;
        if (ch == 'R') rj++;
        nxt[i * m + j] = ri * m + rj;
        lst[ri * m + rj].push_back(i * m + j);
      }
    }
    for (int i = (0); i < (n * m); i += 1) {
      if (vst[i]) continue;
      int f, r;
      f = nxt[i], r = nxt[nxt[i]];
      while (f != r) {
        f = nxt[f];
        r = nxt[nxt[r]];
      }
      vector<int> loop;
      int c = 1;
      loop.push_back(r);
      r = nxt[r];
      while (r != f) {
        loop.push_back(r);
        r = nxt[r];
      }
      c = loop.size();
      fill(cnt, cnt + c, 0);
      for (int k = (0); k < (loop.size()); k += 1) {
        vst[loop[k]] = 1;
        for (int j : lst[loop[k]]) {
          if (j == loop[(k - 1 + c) % c]) continue;
          stack<pair<int, int> > s;
          s.push(make_pair(j, (k - 1 + c) % c));
          while (s.size()) {
            int u = s.top().first, ts = s.top().second;
            s.pop();
            vst[u] = 1;
            if (clr[u] == 0) cnt[ts] = 1;
            for (int v : lst[u]) s.push(make_pair(v, (ts - 1 + c) % c));
          }
        }
        if (clr[loop[k]] == 0) cnt[k] = 1;
      }
      for (int k = (0); k < (c); k += 1) cans += cnt[k];
      lans += c;
    }
    printf("%d %d\n", lans, cans);
  }
}
