#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long T;
  cin >> T;
  while (T--) {
    long long n;
    cin >> n;
    long long m;
    cin >> m;
    vector<string> gr(n);
    vector<bool> ev(n * m);
    for (signed i = 0; i < n; i++) {
      string s;
      cin >> s;
      for (signed j = 0; j < m; j++) ev[i * m + j] = (s[j] == '0');
    }
    for (signed i = 0; i < n; i++) cin >> gr[i];
    vector<vector<long long> > color(n, vector<long long>(m, 0));
    long long c = 0;
    long long vc;
    vector<long long> cycles;
    vector<std::pair<long long, long long> > coor;
    vector<vector<long long> > tin(n, vector<long long>(m));
    function<void(long long, long long)> dfs = [&](long long i, long long j) {
      color[i][j] = vc;
      long long jj, ii;
      if (gr[i][j] == 'R') {
        jj = j + 1;
        ii = i;
      }
      if (gr[i][j] == 'L') {
        jj = j - 1;
        ii = i;
      }
      if (gr[i][j] == 'U') {
        jj = j;
        ii = i - 1;
      }
      if (gr[i][j] == 'D') {
        jj = j;
        ii = i + 1;
      }
      if (color[ii][jj] == vc) {
        cycles[vc - 1] = tin[i][j] - tin[ii][jj] + 1;
        coor[vc - 1] = make_pair(ii, jj);
      } else if (color[ii][jj] == 0) {
        tin[ii][jj] = tin[i][j] + 1;
        dfs(ii, jj);
      } else {
        vc = color[i][j] = color[ii][jj];
      }
    };
    function<void(long long, long long)> dfs1 = [&](long long i, long long j) {
      color[i][j] = vc;
      long long jj, ii;
      if (gr[i][j] == 'R') {
        jj = j + 1;
        ii = i;
      }
      if (gr[i][j] == 'L') {
        jj = j - 1;
        ii = i;
      }
      if (gr[i][j] == 'U') {
        jj = j;
        ii = i - 1;
      }
      if (gr[i][j] == 'D') {
        jj = j;
        ii = i + 1;
      }
      if (color[ii][jj] != vc) dfs1(ii, jj);
    };
    for (signed i = 0; i < n; i++)
      for (signed j = 0; j < m; j++) {
        if (!color[i][j]) {
          tin[i][j] = 1;
          cycles.push_back(0);
          coor.emplace_back(0, 0);
          c++;
          vc = c;
          dfs(i, j);
          if (vc != c) {
            dfs1(i, j);
            c--;
            cycles.pop_back();
            coor.pop_back();
          }
        }
      }
    long long ans1 = 0;
    long long ans2 = 0;
    bool vis[n * m];
    for (signed i = 0; i < n * m; i++) vis[i] = false;
    for (signed i = 0; i < cycles.size(); i++) {
      long long s = cycles[i];
      ans1 += s;
      long long a[s];
      for (signed j = 0; j < s; j++) {
        a[j] = 0;
      }
      function<void(long long, long long, long long)> dfs2 =
          [&](long long i, long long j, long long v) {
            if (vis[m * i + j]) return;
            vis[m * i + j] = true;
            if (ev[i * m + j]) a[v] = 1;
            if (i)
              if (gr[i - 1][j] == 'D') dfs2(i - 1, j, (v + 1) % s);
            if (i != n - 1)
              if (gr[i + 1][j] == 'U') dfs2(i + 1, j, (v + 1) % s);
            if (j)
              if (gr[i][j - 1] == 'R') dfs2(i, j - 1, (v + 1) % s);
            if (j != m - 1)
              if (gr[i][j + 1] == 'L') dfs2(i, j + 1, (v + 1) % s);
          };
      dfs2(coor[i].first, coor[i].second, 0);
      long long res = 0;
      for (signed i = 0; i < s; i++) res += a[i];
      ans2 += res;
    }
    cout << ans1 << ' ' << ans2 << '\n';
  }
}
signed main(signed argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  if (argc > 1 && (string)argv[1] == "local") {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    solve();
    while (cin.peek() != EOF) {
      if (isspace(cin.peek()))
        cin.get();
      else {
        cout << '\n';
        solve();
      }
    }
  } else {
    solve();
  }
  return 0;
}
