#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 2;
int t, n, m, U, V, sum, ans1, ans2;
bool kt[maxn], ch[maxn];
struct T {
  int c, id;
};
vector<T> a[maxn];
vector<int> s[maxn];
void Enter() {
  cin >> m >> n;
  for (int i = 1; i <= m; i++) {
    a[i].clear();
    s[i].clear();
    a[i].push_back({'a', 0});
    s[i].push_back(0);
  }
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) {
      char u;
      cin >> u;
      s[i].push_back(u - '0');
    }
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) {
      char c;
      cin >> c;
      a[i].push_back({c, -1});
    }
}
void Findcir(int u, int v, int id) {
  if (a[u][v].id != -1) {
    U = u;
    V = v;
    sum = id - a[u][v].id;
    return;
  }
  a[u][v].id = id;
  char c = a[u][v].c;
  if (c == 'U') Findcir(u - 1, v, id + 1);
  if (c == 'D') Findcir(u + 1, v, id + 1);
  if (c == 'L') Findcir(u, v - 1, id + 1);
  if (c == 'R') Findcir(u, v + 1, id + 1);
  a[u][v].id = -1;
}
void DFS(int u, int v, int id) {
  a[u][v].id = id;
  if (s[u][v] == 0) {
    if (!kt[id % sum]) {
      kt[id % sum] = true;
      ans2++;
      ans1 -= ch[id % sum];
    }
  } else {
    if (!ch[id % sum] && !kt[id % sum]) {
      ch[id % sum] = true;
      ans1++;
    }
  }
  if (u > 1 && a[u - 1][v].id == -1 && a[u - 1][v].c == 'D')
    DFS(u - 1, v, id + 1);
  if (u < m && a[u + 1][v].id == -1 && a[u + 1][v].c == 'U')
    DFS(u + 1, v, id + 1);
  if (v > 1 && a[u][v - 1].id == -1 && a[u][v - 1].c == 'R')
    DFS(u, v - 1, id + 1);
  if (v < n && a[u][v + 1].id == -1 && a[u][v + 1].c == 'L')
    DFS(u, v + 1, id + 1);
}
void Solve() {
  ans1 = ans2 = 0;
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++)
      if (a[i][j].id == -1) {
        Findcir(i, j, 0);
        fill_n(kt, sum + 1, false);
        fill_n(ch, sum + 1, false);
        DFS(U, V, 0);
      }
  cout << ans1 + ans2 << " " << ans2 << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> t;
  while (t--) {
    Enter();
    Solve();
  }
}
