#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int INF = 1e9;
const long long LINF = INF * (long long)INF + 100;
const double EPS = 1e-7;
const int MAX = 20 * 1000 + 47;
const int MAS = 2 * 1e5 + 10;
const int MOD = 998244353;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<string> s(n);
    for (int i = (0); i < (n); i++) {
      cin >> s[i];
    }
    vector<vector<int>> g(n);
    vector<string> s1(n);
    vector<vector<int>> p(n * m, vector<int>(21));
    for (int i = (0); i < (n); i++) {
      cin >> s1[i];
      for (int j = (0); j < (m); j++) {
        int num = i * m + j;
        if (s1[i][j] == 'R') {
          p[num][0] = num + 1;
        }
        if (s1[i][j] == 'L') {
          p[num][0] = num - 1;
        }
        if (s1[i][j] == 'U') {
          p[num][0] = num - m;
        }
        if (s1[i][j] == 'D') {
          p[num][0] = num + m;
        }
      }
    }
    for (int k = (1); k < (21); k++) {
      for (int i = (0); i < (n * m); i++) {
        p[i][k] = p[p[i][k - 1]][k - 1];
      }
    }
    set<int> all;
    set<int> black;
    for (int i = (0); i < (n); i++)
      for (int j = (0); j < (m); j++) {
        int num = (i * m + j);
        int p1 = p[num][20];
        all.insert(p1);
        if (s[i][j] == '0') {
          black.insert(p1);
        }
      }
    cout << all.size() << " " << black.size() << endl;
  }
}
