#include <bits/stdc++.h>
using namespace std;
long long t, n, m, i, j, r, c;
vector<vector<char> > clr;
vector<vector<char> > s;
vector<vector<int> > parx;
vector<vector<int> > pary;
vector<vector<int> > nparx;
vector<vector<int> > npary;
void solve() {
  cin >> n >> m;
  clr = vector<vector<char> >(n + 1, vector<char>(m + 1));
  s = vector<vector<char> >(n + 1, vector<char>(m + 1));
  parx = vector<vector<int> >(n + 1, vector<int>(m + 1, 1));
  pary = vector<vector<int> >(n + 1, vector<int>(m + 1, 1));
  string str;
  for (i = 1; i <= n; ++i) {
    cin >> str;
    for (j = 1; j <= m; ++j) {
      clr[i][j] = str[j - 1];
    }
  }
  for (i = 1; i <= n; ++i) {
    cin >> str;
    for (j = 1; j <= m; ++j) {
      s[i][j] = str[j - 1];
      if (s[i][j] == 'U') {
        r = i - 1;
        c = j;
      } else if (s[i][j] == 'D') {
        r = i + 1;
        c = j;
      } else if (s[i][j] == 'L') {
        r = i;
        c = j - 1;
      } else {
        r = i;
        c = j + 1;
      }
      parx[i][j] = r;
      pary[i][j] = c;
    }
  }
  nparx = vector<vector<int> >(n + 1, vector<int>(m + 1, -1));
  npary = vector<vector<int> >(n + 1, vector<int>(m + 1, -1));
  for (i = 1; i <= 24; ++i) {
    for (r = 1; r <= n; ++r) {
      for (c = 1; c <= m; ++c) {
        nparx[r][c] = parx[parx[r][c]][pary[r][c]];
        npary[r][c] = pary[parx[r][c]][pary[r][c]];
      }
    }
    for (r = 1; r <= n; ++r) {
      for (c = 1; c <= m; ++c) {
        parx[r][c] = nparx[r][c];
        pary[r][c] = npary[r][c];
      }
    }
  }
  long long can = 0, blk = 0;
  vector<vector<bool> > can_rch =
      vector<vector<bool> >(n + 1, vector<bool>(m + 1, false));
  vector<vector<bool> > blk_can_rch =
      vector<vector<bool> >(n + 1, vector<bool>(m + 1, false));
  for (r = 1; r <= n; ++r) {
    for (c = 1; c <= m; ++c) {
      can_rch[parx[r][c]][pary[r][c]] = true;
      if (clr[r][c] == '0') {
        blk_can_rch[parx[r][c]][pary[r][c]] = true;
      }
    }
  }
  for (r = 1; r <= n; ++r) {
    for (c = 1; c <= m; ++c) {
      if (can_rch[r][c]) {
        can++;
      }
      if (blk_can_rch[r][c]) {
        blk++;
      }
    }
  }
  cout << can << ' ' << blk << endl;
}
int main(int argc, char const *argv[]) {
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
