#include <bits/stdc++.h>
using namespace std;
template <class T>
bool umin(T& a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool umax(T& a, T b) {
  return a < b ? (a = b, true) : false;
}
long long POW(long long a, long long p, long long M) {
  if (!p) return 1LL;
  long long T = POW(a, p / 2, M);
  T = T * T % M;
  if (p & 1) T = T * (a % M) % M;
  return T;
}
long long SQRT(long long a) {
  long long b = (long long)sqrtl(((double)a) + 0.5);
  while (b * b < a) ++b;
  while (b * b > a) --b;
  return (b * b == a) ? b : -1;
}
const long long MOD = 1e9 + 7;
vector<int> cycle;
void dfs(int node, vector<vector<int>>& graph, vector<int>& vis) {
  vis[node] = 0;
  for (auto it : graph[node]) {
    if (vis[it] == -1)
      dfs(it, graph, vis);
    else if (vis[it] == 0)
      cycle.push_back(it);
  }
  vis[node] = 1;
}
int len;
map<int, int> mm;
void bfs(int node, vector<vector<int>>& graph, vector<int>& vis,
         vector<int>& colour, int dist) {
  vis[node] = 1;
  if (colour[node] == 0) mm[dist] = 1;
  for (auto it : graph[node]) {
    if (vis[it] == -1)
      bfs(it, graph, vis, colour, dist + 1);
    else
      len = dist + 1;
  }
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;
  int t;
  cin >> t;
  while (t--) {
    cycle.clear();
    int n, m;
    cin >> n >> m;
    vector<string> v(n);
    vector<string> col(n);
    for (int i = 0; i < n; i++) cin >> col[i];
    for (int i = 0; i < n; i++) cin >> v[i];
    int sz = n * m;
    vector<int> colour(sz + 5);
    vector<vector<int>> graph(sz + 5);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        int x, y;
        x = i;
        y = j;
        if (v[i][j] == 'L')
          y--;
        else if (v[i][j] == 'R')
          y++;
        else if (v[i][j] == 'U')
          x--;
        else
          x++;
        int targ = m * x + y + 1;
        int src = m * i + j + 1;
        graph[targ].push_back(src);
        colour[src] = col[i][j] - '0';
      }
    vector<int> vis(sz + 5);
    for (int i = 1; i <= sz; i++) vis[i] = -1;
    for (int i = 1; i <= sz; i++) {
      if (vis[i] == -1) dfs(i, graph, vis);
    }
    for (int i = 1; i <= sz; i++) vis[i] = -1;
    int ans, black;
    ans = black = 0;
    for (auto it : cycle) {
      if (vis[it] == -1) {
        len = -1;
        bfs(it, graph, vis, colour, 0);
        ans += len;
        map<int, int> ff;
        for (auto it : mm) ff[it.first % len]++;
        black += ff.size();
        mm.clear();
      }
    }
    cout << ans << " " << black << endl;
  }
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s\n";
  return 0;
}
