#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 0;
  cin >> t;
  while (t--) {
    int m, n;
    cin >> m >> n;
    vector<string> s(m);
    vector<string> c(m);
    for (int i = 0; i < m; i++) cin >> c[i];
    for (int i = 0; i < m; i++) cin >> s[i];
    vector<int> deg(m * n, 0);
    vector<int> f(m * n);
    vector<vector<int>> e(m * n);
    for (int i = 0; i < m; i++)
      for (int j = 0; j < n; j++) {
        int x = i, y = j;
        if (s[x][y] == 'L')
          y--;
        else if (s[x][y] == 'R')
          y++;
        else if (s[x][y] == 'U')
          x--;
        else if (s[x][y] == 'D')
          x++;
        f[i * n + j] = x * n + y;
      }
    for (int x = 0; x < m * n; x++) deg[f[x]]++;
    vector<int> q;
    for (int x = 0; x < m * n; x++)
      if (deg[x] == 0) q.push_back(x);
    while (q.size()) {
      int x = q.back();
      q.pop_back();
      if (--deg[f[x]] == 0) q.push_back(f[x]);
      e[f[x]].push_back(x);
    }
    vector<pair<int, int>> qu;
    int ans = 0, ans2 = 0;
    for (int i = 0; i < m * n; i++)
      if (deg[i] > 0) {
        int len = 1;
        for (int x = f[i]; x != i; ++len, x = f[x]) e[f[x]].push_back(x);
        ans += len;
        vector<int> kt(len, 0);
        qu.push_back({i, 0});
        while (qu.size()) {
          int j = qu.back().first;
          int d = qu.back().second;
          --deg[j];
          if (c[j / n][j % n] == '0') kt[d % len] = 1;
          qu.pop_back();
          for (int x : e[j]) {
            qu.push_back({x, d + 1});
          }
        }
        for (int i : kt) ans2 += i;
      }
    cout << ans << " " << ans2 << "\n";
  }
}
