#include <bits/stdc++.h>
using namespace std;
template <typename _t>
inline void read(_t &x) {
  x = 0;
  _t fu = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') fu = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch & 15);
    ch = getchar();
  }
  x *= fu;
}
long long gcd(long long x, long long y) { return !x ? y : gcd(y % x, x); }
void exgcd(long long a, long long b, long long &x, long long &y) {
  if (!b) {
    x = 1;
    y = 0;
    return;
  }
  exgcd(b, a % b, y, x);
  y -= a / b * x;
}
struct Mart {
  int a, b, c, d;
  Mart operator*(Mart &m) {
    Mart temp;
    temp.a = a * m.a + b * m.c;
    temp.b = a * m.b + b * m.d;
    temp.c = c * m.a + d * m.c;
    temp.d = c * m.b + d * m.d;
    return temp;
  }
  Mart() { a = b = c = d = 0; }
  Mart(int x[4]) {
    a = x[0];
    b = x[1];
    c = x[2];
    d = x[3];
  }
  Mart(int u, int v, int w, int x) {
    a = u;
    b = v;
    c = w;
    d = x;
  }
  int det() { return a * d - b * c; }
};
const int maxn = 1e6 + 10;
int n, m;
vector<string> vec, c;
int log2(int n) {
  int wei = 0, res = 1;
  while (res < n) {
    res *= 2;
    wei++;
  }
  return wei;
}
int nex[maxn][21];
set<int> s1, s2;
void ini() {
  vec.clear();
  c.clear();
  s1.clear();
  s2.clear();
}
int main() {
  cin.tie(0);
  string temp;
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d %d", &n, &m);
    ini();
    for (int i = 0; i < n; i++) {
      cin >> temp;
      c.push_back(temp);
    }
    for (int i = 0; i < n; i++) {
      cin >> temp;
      vec.push_back(temp);
    }
    n *= m;
    int W = log2(n);
    for (int pos = 0; pos < n; pos++) {
      int x = pos / m, y = pos % m;
      switch (vec[x][y]) {
        case 'L':
          nex[pos][0] = pos - 1;
          break;
        case 'R':
          nex[pos][0] = pos + 1;
          break;
        case 'U':
          nex[pos][0] = pos - m;
          break;
        case 'D':
          nex[pos][0] = pos + m;
          break;
      }
    }
    for (int wei = 1; wei <= W; wei++) {
      for (int pos = 0; pos < n; pos++) {
        nex[pos][wei] = nex[nex[pos][wei - 1]][wei - 1];
      }
    }
    for (int i = 0; i < n; i++) {
      s1.insert(nex[i][W]);
      if (c[i / m][i % m] == '0') s2.insert(nex[i][W]);
    }
    printf("%d %d\n", (int)s1.size(), (int)s2.size());
  }
  return 0;
}
