#include <bits/stdc++.h>
using namespace std;
vector<string> s;
map<pair<int, int>, int> m1;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    int arr[n + 1][m + 1];
    vector<vector<vector<pair<int, int>>>> lft(
        2, vector<vector<pair<int, int>>>(
               n + 5, vector<pair<int, int>>(m + 5, pair<int, int>(0, 0))));
    for (int i = 1; i <= n; i++) {
      string s;
      cin >> s;
      for (int j = 1; j <= m; j++) {
        arr[i][j] = (s[j - 1] == '0');
      }
    }
    s.clear();
    s.push_back("#");
    for (int i = 1; i <= n; i++) {
      string tempstr = "";
      tempstr += '#';
      string x;
      cin >> x;
      tempstr += x;
      s.push_back(tempstr);
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (s[i][j] == 'U') {
          lft[0][i][j] = make_pair(i - 1, j);
        }
        if (s[i][j] == 'D') {
          lft[0][i][j] = make_pair(i + 1, j);
        }
        if (s[i][j] == 'L') {
          lft[0][i][j] = make_pair(i, j - 1);
        }
        if (s[i][j] == 'R') {
          lft[0][i][j] = make_pair(i, j + 1);
        }
      }
    }
    int f = 0;
    int se = 1;
    for (int x = 1; x <= 30; x++) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
          pair<int, int> par = lft[f][i][j];
          lft[se][i][j] = lft[f][par.first][par.second];
        }
      }
      swap(se, f);
    }
    m1.clear();
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (!arr[i][j]) {
          continue;
        }
        m1[lft[0][i][j]]++;
      }
    }
    int ans2 = m1.size();
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (arr[i][j]) {
          continue;
        }
        m1[lft[0][i][j]]++;
      }
    }
    cout << m1.size() << " " << ans2 << endl;
  }
}
