#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1000000 + 10;
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int n, m, ans1, ans2;
string c;
int r[maxn];
char s[maxn];
bool vis[maxn];
int dis[maxn];
int a[maxn];
int paint[maxn][2];
int len[maxn], nl;
set<pair<int, int> > g;
inline int encode(int x, int y) { return x * m + y; }
inline void decode(int v, int &x, int &y) {
  x = v / m;
  y = v % m;
}
void print(int a[], int n, int st = 0) {
  for (int i = 0; i < n; i++) {
    if (i != 0) printf(" ");
    printf("%d", a[i + st]);
  }
  printf("\n");
}
void init() {
  char ch;
  scanf("%d%d", &n, &m);
  c = "";
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    c += s;
  }
  int num = 0;
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    for (int j = 0; j < m; j++) {
      if (s[j] == 'U') r[num++] = 0;
      if (s[j] == 'D') r[num++] = 1;
      if (s[j] == 'L') r[num++] = 2;
      if (s[j] == 'R') r[num++] = 3;
    }
  }
}
void doit() {
  int tot = encode(n - 1, m - 1), cur;
  int st, ed, cnt;
  for (int i = 0; i <= tot; i++) {
    vis[i] = false;
    dis[i] = -1;
    paint[i][0] = paint[i][1] = -1;
  }
  nl = 0;
  ans1 = ans2 = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int xx = i, yy = j;
      cur = encode(i, j);
      if (vis[cur]) continue;
      st = cur;
      cnt = 0;
      while (!vis[cur]) {
        vis[cur] = true;
        a[cnt] = cur;
        dis[cur] = cnt;
        xx += dx[r[cur]];
        yy += dy[r[cur]];
        cur = encode(xx, yy);
        cnt++;
      }
      if (paint[cur][0] >= 0) {
        int cc = paint[cur][1];
        for (int k = cnt - 1; k >= 0; k--) {
          paint[a[k]][0] = paint[cur][0];
          cc = cc - 1;
          if (cc < 0) cc = len[paint[cur][0]] - 1;
          paint[a[k]][1] = cc;
        }
      } else {
        len[nl] = cnt - 1 - dis[cur] + 1;
        ans1 += len[nl];
        for (int k = 0; k < len[nl]; k++) {
          paint[a[k + dis[cur]]][0] = nl;
          paint[a[k + dis[cur]]][1] = k;
        }
        int cc = 0;
        for (int k = dis[cur] - 1; k >= 0; k--) {
          paint[a[k]][0] = nl;
          cc = cc - 1;
          if (cc < 0) cc = len[nl] - 1;
          paint[a[k]][1] = cc;
        }
        nl++;
      }
    }
  }
  g.clear();
  for (int i = 0; i <= tot; i++) {
    if (c[i] == '0' && g.find(make_pair(paint[i][0], paint[i][1])) == g.end()) {
      g.insert(make_pair(paint[i][0], paint[i][1]));
    }
  }
  ans2 = g.size();
  printf("%d %d\n", ans1, ans2);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    init();
    doit();
  }
  return 0;
}
