#include <bits/stdc++.h>
using namespace std;
map<char, pair<int, int>> mm;
int n, m, ret, peeps, f, ans1;
vector<int> dsu;
vector<vector<int>> dist, in;
vector<vector<bool>> cyc, vis, num;
vector<string> di, bw;
void GiveNumber(int i, int j, int f) {
  cyc[i][j] = true;
  in[i][j] = f;
  dist[i][j] = ret++;
  int newi = i + mm[di[i][j]].first;
  int newj = j + mm[di[i][j]].second;
  if (!cyc[newi][newj]) GiveNumber(newi, newj, f);
}
void dfs1(int i, int j) {
  vis[i][j] = true;
  int newi = i + mm[di[i][j]].first;
  int newj = j + mm[di[i][j]].second;
  if (!vis[newi][newj])
    dfs1(newi, newj);
  else if (!cyc[newi][newj]) {
    ret = 0;
    GiveNumber(newi, newj, ++f);
    num.push_back(vector<bool>(ret, 0));
    dsu.push_back(ret);
    ans1 += ret;
  }
  cyc[i][j] = true;
}
void dfs2(int i, int j) {
  int newi = i + mm[di[i][j]].first;
  int newj = j + mm[di[i][j]].second;
  if (!in[newi][newj]) dfs2(newi, newj);
  in[i][j] = in[newi][newj];
  dist[i][j] = (dist[newi][newj] - 1 + dsu[in[i][j]]) % dsu[in[i][j]];
  if (bw[i][j] == '0') num[in[i][j] - 1][dist[i][j]] = 1;
}
int main() {
  int t;
  cin >> t;
  mm['U'] = make_pair(-1, 0);
  mm['R'] = make_pair(0, 1);
  mm['D'] = make_pair(1, 0);
  mm['L'] = make_pair(0, -1);
  while (t--) {
    cin >> n >> m;
    f = 0, ans1 = 0, ret = 0, peeps = 0;
    bw.clear();
    bw.resize(n);
    di.clear();
    di.resize(n);
    vis.clear();
    vis.resize(n, vector<bool>(m, 0));
    cyc.clear();
    cyc.resize(n, vector<bool>(m, 0));
    num.clear();
    dsu.clear();
    dsu.resize(1);
    in.clear();
    in.resize(n, vector<int>(m, 0));
    dist.clear();
    dist.resize(n, vector<int>(m, 0));
    for (int i = 0; i < n; ++i) cin >> bw[i];
    for (int i = 0; i < n; ++i) cin >> di[i];
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        ret = 0;
        if (!vis[i][j]) dfs1(i, j);
      }
    }
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
        if (in[i][j] == 0)
          dfs2(i, j);
        else if (bw[i][j] == '0')
          num[in[i][j] - 1][dist[i][j]] = 1;
    for (int i = 0; i < num.size(); ++i) {
      for (int j = 0; j < num[i].size(); ++j) {
        if (num[i][j] == 1) peeps++;
      }
    }
    printf("%d %d\n", ans1, peeps);
  }
}
