#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  bool first = true;
  for (const auto& j : v) {
    if (!first) {
      os << " ";
    }
    first = false;
    os << j;
  }
  return os;
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
  for (auto& elem : v) {
    is >> elem;
  }
  return is;
}
vector<string> c, a;
vector<vector<int> > b;
vector<vector<int> > blen;
vector<vector<int> > bidx;
int n, m;
bool dfs(int y, int x, int len) {
  if (b[y][x] != 0) {
    if (b[y][x] < 0) {
      blen[y][x] = len + b[y][x];
      bidx[y][x] = 1;
      b[y][x] = (1 + y) * (m + 1) + (1 + x);
      return true;
    }
    return false;
  }
  int nx = x;
  int ny = y;
  if (a[y][x] == 'R') {
    nx++;
  } else if (a[y][x] == 'L') {
    nx--;
  } else if (a[y][x] == 'U') {
    ny--;
  } else if (a[y][x] == 'D') {
    ny++;
  } else {
    exit(1);
  }
  b[y][x] = -len;
  bool f = dfs(ny, nx, len + 1);
  if (b[y][x] < 0) {
    bidx[y][x] = bidx[ny][nx] + 1;
    b[y][x] = b[ny][nx];
    if (f) {
      blen[y][x] = blen[ny][nx];
    } else {
      blen[y][x] = -abs(blen[ny][nx]);
    }
    return f;
  } else {
    return false;
  }
}
void solve() {
  cin >> n >> m;
  c.resize(n);
  a.resize(n);
  b.assign(n, vector<int>(m, 0));
  blen.assign(n, vector<int>(m, 0));
  bidx.assign(n, vector<int>(m, 0));
  unordered_map<int, unordered_set<int> > d;
  cin >> c;
  cin >> a;
  int ans1 = 0;
  int ans2 = 0;
  for (int j = 0; j < n; j++) {
    for (int i = 0; i < m; i++) {
      if (b[j][i] == 0) {
        dfs(j, i, 1);
      }
    }
  }
  for (int j = 0; j < n; j++) {
    for (int i = 0; i < m; i++) {
      if (blen[j][i] > 0) {
        ans1++;
      }
      if (c[j][i] == '0') {
        d[b[j][i]].insert(bidx[j][i] % abs(blen[j][i]));
      }
    }
  }
  for (auto it : d) {
    ans2 += it.second.size();
  }
  cout << ans1 << ' ' << ans2;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  if (1) {
    int tests;
    cin >> tests;
    while (tests--) {
      solve();
      cout << "\n";
    }
  } else {
    solve();
    cout << endl;
  }
  return 0;
}
