#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
long long n, m, k, t;
const long long maxn = 1e6 + 100;
vector<vector<char> > A;
vector<vector<long long> > C;
vector<vector<long long> > used;
vector<pair<long long, long long> > cycle;
long long color = 1;
long long sz[maxn];
pair<long long, long long> root[maxn];
vector<vector<long long> > dist;
long long dfs(long long first, long long second) {
  cycle.push_back(make_pair(first, second));
  if (used[first][second] != 0) {
    return used[first][second];
  }
  used[first][second] = color;
  if (A[first][second] == 'U') {
    return dfs(first - 1, second);
  }
  if (A[first][second] == 'D') {
    return dfs(first + 1, second);
  }
  if (A[first][second] == 'L') {
    return dfs(first, second - 1);
  }
  return dfs(first, second + 1);
}
bool in(long long first, long long second) {
  return first >= 0 && second >= 0 && first < n && second < m;
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long q, w, e, a, b;
  cin >> t;
  for (long long e1 = 0; e1 < t; e1++) {
    cin >> n >> m;
    color = 1;
    A.resize(n);
    C.resize(n);
    used.resize(n);
    dist.resize(n);
    for (q = 0; q < n * m; q++) {
      sz[q] = 0;
    }
    for (q = 0; q < n; q++) {
      A[q].resize(m);
      C[q].resize(m);
      dist[q].resize(m);
      used[q].resize(m);
      for (w = 0; w < m; w++) {
        dist[q][w] = -1;
        used[q][w] = 0;
      }
    }
    for (q = 0; q < n; q++) {
      string s;
      cin >> s;
      for (w = 0; w < m; w++) {
        C[q][w] = s[w] - '0';
      }
    }
    for (q = 0; q < n; q++) {
      string s;
      cin >> s;
      for (w = 0; w < m; w++) {
        A[q][w] = s[w];
      }
    }
    queue<pair<long long, long long> > Q;
    for (q = 0; q < n; q++) {
      for (w = 0; w < m; w++) {
        if (!used[q][w]) {
          cycle.clear();
          long long c = dfs(q, w);
          for (e = 0; e < cycle.size(); e++) {
            used[cycle[e].first][cycle[e].second] = c;
          }
          if (c == color) {
            for (e = 0; e < cycle.size(); e++) {
              if (cycle[e] == cycle.back()) {
                root[c] = cycle.back();
                Q.push(root[c]);
                dist[root[c].first][root[c].second] = 0;
                sz[c] = cycle.size() - e - 1;
                break;
              }
            }
          }
          color++;
        }
      }
    }
    while (!Q.empty()) {
      pair<long long, long long> p = Q.front();
      Q.pop();
      long long first = p.first, second = p.second;
      for (long long dx = -1; dx < 2; dx++) {
        for (long long dy = -1; dy < 2; dy++) {
          if (!((dx == 0) ^ (dy == 0))) {
            continue;
          }
          long long ny = second + dy, nx = first + dx;
          if (!in(nx, ny)) {
            continue;
          }
          if (dist[nx][ny] != -1) {
            continue;
          }
          if (dx == 1 && A[nx][ny] == 'U') {
            dist[nx][ny] = dist[first][second] + 1;
            Q.push(make_pair(nx, ny));
          }
          if (dx == -1 && A[nx][ny] == 'D') {
            dist[nx][ny] = dist[first][second] + 1;
            Q.push(make_pair(nx, ny));
          }
          if (dy == -1 && A[nx][ny] == 'R') {
            dist[nx][ny] = dist[first][second] + 1;
            Q.push(make_pair(nx, ny));
          }
          if (dy == 1 && A[nx][ny] == 'L') {
            dist[nx][ny] = dist[first][second] + 1;
            Q.push(make_pair(nx, ny));
          }
        }
      }
    }
    vector<vector<long long> > have;
    for (q = 0; q < color; q++) {
      vector<long long> temp;
      have.push_back(temp);
      have[q].resize(sz[q]);
    }
    for (q = 0; q < n; q++) {
      for (w = 0; w < m; w++) {
        if (C[q][w] == 1) {
          continue;
        }
        long long c = used[q][w];
        long long i = dist[q][w] % sz[c];
        have[c][i] = 1;
      }
    }
    long long ans1 = 0, ans2 = 0;
    for (q = 0; q < color; q++) {
      ans1 += sz[q];
      for (w = 0; w < sz[q]; w++) {
        ans2 += have[q][w];
      }
    }
    cout << ans1 << " " << ans2 << endl;
  }
  return 0;
}
