#include <bits/stdc++.h>
using namespace std;
const int N = 1e+6 + 5;
int n, m;
int takeans[N], nxt[N], colpt[N];
set<int> bval[N];
pair<int, int> dis[N], dis1[N];
int pti(int x, int y) { return (x - 1) * m + y; }
pair<int, int> itp(int x) {
  pair<int, int> ans;
  ans.first = (x - 1) / m + 1;
  ans.second = (x - 1) % m + 1;
  return ans;
}
void dfs(int u) {
  if (dis[nxt[u]].second == dis[u].second) {
    takeans[u] = abs(dis[u].first - dis[nxt[u]].first) + 1;
    dis1[u].first = 0;
    dis1[u].second = u;
  } else {
    if (dis[nxt[u]].second == 0) {
      dis[nxt[u]].first = dis[u].first + 1;
      dis[nxt[u]].second = dis[u].second;
      dfs(nxt[u]);
    }
    dis1[u] = dis1[nxt[u]];
    dis1[u].first++;
  }
  if (colpt[u] == 0)
    bval[dis1[u].second].insert((dis1[u].first) % takeans[dis1[u].second]);
  return;
}
int main() {
  map<char, int> f, s;
  f['U'] = -1;
  f['D'] = 1;
  s['R'] = 1;
  s['L'] = -1;
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    char col[n + 1][m + 1], cusmap[n + 1][m + 1];
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) cin >> col[i][j];
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) cin >> cusmap[i][j];
    for (int i = 1; i <= m * n; i++) {
      dis[i].first = dis[i].second = 0;
      dis1[i].first = dis1[i].second = 0;
      bval[i].clear();
      colpt[i] = takeans[i] = nxt[i] = 0;
    }
    for (int v = 1; v <= m * n; v++) {
      pair<int, int> pt = itp(v);
      nxt[v] = pti(pt.first + f[cusmap[pt.first][pt.second]],
                   pt.second + s[cusmap[pt.first][pt.second]]);
      colpt[v] = col[pt.first][pt.second] - '0';
    }
    for (int v = 1; v <= m * n; v++) {
      if (dis[v].second == 0) {
        dis[v].first = 0;
        dis[v].second = v;
        dfs(v);
      }
    }
    int ans1 = 0, ans2 = 0;
    for (int i = 1; i <= m * n; i++) {
      ans1 += takeans[i];
      ans2 += min(takeans[i], (int)bval[i].size());
    }
    cout << ans1 << " " << ans2 << "\n";
  }
}
