#include <bits/stdc++.h>
using namespace std;
int nxt[1000010][21];
vector<string> s, c;
map<int, vector<pair<int, int>>> eq;
int n, m;
inline int tr(int i, int j) { return m * (i - 1) + j; }
int get_nxt(int i, int j) {
  if (s[i][j] == 'R') {
    return tr(i, j + 1);
  }
  if (s[i][j] == 'L') {
    return tr(i, j - 1);
  }
  if (s[i][j] == 'U') {
    return tr(i - 1, j);
  }
  if (s[i][j] == 'D') {
    return tr(i + 1, j);
  }
  return 0;
}
int main() {
  int t, i, j, k, flag, lg_max, idx, b;
  string str;
  vector<pair<int, int>> v;
  map<int, vector<pair<int, int>>>::iterator it;
  cin >> t;
  while (t--) {
    eq.clear();
    s.clear();
    c.clear();
    cin >> n >> m;
    c.push_back("");
    for (i = 1; i <= n; i++) {
      cin >> str;
      str = '#' + str;
      c.push_back(str);
    }
    s.push_back("");
    for (i = 1; i <= n; i++) {
      cin >> str;
      str = '#' + str;
      s.push_back(str);
    }
    lg_max = ceil(log(n * m) / log(2));
    for (k = 0; k <= lg_max; k++) {
      for (i = 1; i <= n; i++) {
        for (j = 1; j <= m; j++) {
          idx = tr(i, j);
          if (k == 0) {
            nxt[idx][0] = get_nxt(i, j);
          } else {
            nxt[idx][k] = nxt[nxt[idx][k - 1]][k - 1];
          }
        }
      }
    }
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        idx = tr(i, j);
        eq[nxt[idx][lg_max]].push_back(make_pair(i, j));
      }
    }
    b = 0;
    for (it = eq.begin(); it != eq.end(); it++) {
      v = (*it).second;
      flag = 0;
      for (i = 0; i < v.size(); i++) {
        if (c[v[i].first][v[i].second] == '0') {
          flag = 1;
          break;
        }
      }
      if (flag == 1) b++;
    }
    cout << eq.size() << " " << b << endl;
  }
  return 0;
}
