#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
vector<string> v1, v2;
vector<vector<int>> vis, nxt;
int n, m, k, lognm;
void getnext(int x, int y, int &nx, int &ny) {
  if (v2[x][y] == 'U')
    nx = x - 1, ny = y;
  else if (v2[x][y] == 'D')
    nx = x + 1, ny = y;
  else if (v2[x][y] == 'L')
    nx = x, ny = y - 1;
  else if (v2[x][y] == 'R')
    nx = x, ny = y + 1;
}
void dfs(int x, int y) {
  vis[x][y] = 1;
  int nx, ny;
  getnext(x, y, nx, ny);
  assert(nx >= 0 && nx < n && ny >= 0 && ny < m);
  if (!vis[nx][ny]) dfs(nx, ny);
  nxt[x * m + y][0] = nx * m + ny;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    k = n * m;
    lognm = ceil(log(n * m) / log(2)) + 1;
    v1 = v2 = vector<string>(n);
    for (auto &it : v1) cin >> it;
    for (auto &it : v2) cin >> it;
    vis = vector<vector<int>>(n, vector<int>(m));
    ;
    nxt = vector<vector<int>>(n * m, vector<int>(lognm));
    ;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (!vis[i][j]) dfs(i, j);
      }
    }
    for (int deg = 1; deg < lognm; deg++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          int id = i * m + j;
          nxt[id][deg] = nxt[nxt[id][deg - 1]][deg - 1];
        }
      }
    }
    vector<vector<int>> black, white;
    black = white = vector<vector<int>>(n, vector<int>(m));
    ;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int v = i * m + j, to = v;
        for (int deg = 0; deg < lognm; deg++) {
          if ((k >> deg) & 1) {
            to = nxt[to][deg];
          }
        }
        if (v1[i][j] == '0')
          black[to / m][to % m]++;
        else
          white[to / m][to % m]++;
      }
    }
    int ans1, ans2;
    ans1 = ans2 = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (black[i][j])
          ans1++, ans2++;
        else if (white[i][j])
          ans1++;
      }
    }
    cout << ans1 << ' ' << ans2 << '\n';
  }
  return 0;
}
