#include <bits/stdc++.h>
using namespace std;
int r, c;
string str_col[1000006];
string str_dir[1000006];
vector<int> e[1000006], rev[1000006];
int col[1000006], vis[1000006], vis2[1000006];
int cyc_len, black_cnt;
inline int id(int x, int y) { return c * x + y; }
vector<int> st;
int got[1000006];
void dfs2(int v, int mod, int d = 0) {
  if (0) cerr << "vis2 " << v << ' ' << col[v] << '\n';
  vis2[v] = 1;
  got[d] |= col[v];
  for (int u : rev[v]) {
    if (not vis2[u]) dfs2(u, mod, (d + 1) % mod);
  }
}
void dfs(int v) {
  if (vis[v] or vis2[v]) return;
  if (0) cerr << "dfs " << v << '\n';
  vis[v] = 1;
  st.push_back(v);
  for (int u : e[v]) {
    if (not vis[u])
      dfs(u);
    else {
      int cyc = st.end() - find(st.begin(), st.end(), u);
      if (cyc == 0) continue;
      fill(got, got + cyc, 0);
      if (0) cerr << "call cyc " << v << ' ' << u << '\n';
      dfs2(u, cyc);
      black_cnt += accumulate(got, got + cyc, 0);
      cyc_len += cyc;
    }
  }
  st.pop_back();
}
void solve() {
  cin >> r >> c;
  for (int i = 0; i < r; i++) cin >> str_col[i];
  for (int i = 0; i < r; i++) cin >> str_dir[i];
  for (int x = 0; x < r; x++)
    for (int y = 0; y < c; y++) {
      int nx = x, ny = y;
      if (str_dir[x][y] == 'U') nx--;
      if (str_dir[x][y] == 'D') nx++;
      if (str_dir[x][y] == 'L') ny--;
      if (str_dir[x][y] == 'R') ny++;
      e[id(x, y)].push_back(id(nx, ny));
      rev[id(nx, ny)].push_back(id(x, y));
      if (0) cerr << "rev " << id(nx, ny) << ' ' << id(x, y) << '\n';
      col[id(x, y)] = str_col[x][y] == '0';
    }
  int n = r * c;
  for (int i = 0; i < n; i++) {
    if (not vis[i] and not vis2[i]) {
      if (0) cerr << "call dfs " << i << '\n';
      dfs(i);
    }
  }
  cout << cyc_len << ' ' << black_cnt << '\n';
}
void reset() {
  for (int i = 0; i < r * c; i++) {
    e[i].clear();
    rev[i].clear();
    vis[i] = vis2[i] = cyc_len = black_cnt = 0;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  cin >> t;
  for (int no = 1; no <= t; no++) {
    solve();
    reset();
  }
}
