#include <bits/stdc++.h>
using namespace std;
clock_t start;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
template <typename T>
void _F(const char *name, T arg1) {
  cerr << name << " = " << arg1 << '\n';
}
template <typename T, typename... Args>
void _F(const char *names, T arg1, Args... args) {
  const char *name = strchr(names, ',');
  cerr.write(names, name - names) << " = " << arg1 << '\n';
  _F(name + 2, args...);
}
template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &q) {
  in >> q.first >> q.second;
  return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> &q) {
  out << q.first << " " << q.second;
  return out;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2) {
  return {p1.first + p2.first, p1.second + p2.second};
}
template <typename T1, typename T2>
pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2) {
  return {p1.first - p2.first, p1.second - p2.second};
}
int n, m;
int root, A, B;
vector<pair<int, int> > tmp;
vector<vector<bool> > vis;
vector<vector<bool> > cur;
vector<vector<int> > dis;
vector<vector<int> > siz;
vector<vector<int> > r;
vector<vector<char> > c;
vector<vector<char> > path;
pair<int, int> nex(int i, int j) {
  char ch = path[i][j];
  i += (ch == 'D' ? 1 : (ch == 'U' ? -1 : 0));
  j += (ch == 'L' ? -1 : (ch == 'R' ? 1 : 0));
  return make_pair(i, j);
}
bool check(int i, int j) { return (i >= n || j >= m || i < 0 || j < 0); }
void dfs(int i, int j) {
  if (check(i, j)) return;
  if (cur[i][j]) {
    pair<int, int> nx = nex(i, j);
    dis[i][j] = 0;
    int cnt = 1;
    vector<pair<int, int> > cyc;
    cyc.push_back({i, j});
    r[i][j] = root;
    while (nx != make_pair(i, j)) {
      cyc.push_back(nx);
      dis[nx.first][nx.second] = cnt;
      r[nx.first][nx.second] = root;
      cnt++;
      nx = nex(nx.first, nx.second);
    }
    for (auto u : cyc) siz[u.first][u.second] = (int)((cyc).size());
    A += (int)((cyc).size());
    root++;
  }
  if (vis[i][j] || cur[i][j]) return;
  cur[i][j] = 1, vis[i][j] = 1;
  tmp.push_back({i, j});
  int ii = nex(i, j).first, jj = nex(i, j).second;
  dfs(ii, jj);
  if (dis[i][j] == -1) {
    dis[i][j] = (dis[ii][jj] - 1 + siz[ii][jj]) % siz[ii][jj];
    siz[i][j] = siz[ii][jj];
    r[i][j] = r[ii][jj];
  }
}
void solve() {
  cin >> n >> m;
  vis.resize(n);
  path.resize(n);
  cur.resize(n);
  c.resize(n);
  dis.resize(n);
  r.resize(n);
  siz.resize(n);
  for (int i = 0; i < n; i++) {
    vis[i].resize(m);
    path[i].resize(m);
    cur[i].resize(m);
    c[i].resize(m);
    dis[i].resize(m);
    r[i].resize(m);
    siz[i].resize(m);
    for (int j = 0; j < m; j++) {
      siz[i][j] = vis[i][j] = cur[i][j] = 0;
      r[i][j] = dis[i][j] = -1;
      cin >> c[i][j];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) cin >> path[i][j];
  }
  root = 0, A = 0, B = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (vis[i][j]) continue;
      dfs(i, j);
      for (auto u : tmp) cur[u.first][u.second] = 0;
      tmp.clear();
    }
  }
  set<int> s[n * m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (c[i][j] == '0') s[r[i][j]].insert(dis[i][j]);
    }
  }
  for (int i = 0; i < n * m; i++) {
    B += (int)((s[i]).size());
  }
  cout << A << " " << B << '\n';
  for (int i = 0; i < n; i++) {
    vis[i].clear();
    path[i].clear();
    cur[i].clear();
    c[i].clear();
    dis[i].clear();
    r[i].clear();
    siz[i].clear();
  }
  vis.clear();
  path.clear();
  cur.clear();
  c.clear();
  dis.clear();
  r.clear();
  siz.clear();
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  start = clock();
  cout << fixed << setprecision(12);
  cin >> t;
  for (int i = 1; i <= t; ++i) {
    solve();
  }
  return 0;
}
