#include <bits/stdc++.h>
using namespace std;
template <class T>
using vec = std::vector<T>;
template <class T>
using vec2d = std::vector<std::vector<T>>;
template <class T>
vec2d<T> makeVec2d(const int n1, const int n2) {
  return vec2d<T>(n1, vec<T>(n2));
}
template <class T>
vec2d<T> makeVec2d(const int n1, const int n2, const T initValue) {
  return vec2d<T>(n1, vec<T>(n2, initValue));
}
bool __hack = std::ios::sync_with_stdio(false);
auto __hack1 = cin.tie(nullptr);
namespace template_util {
constexpr int bytecount(uint64_t x) { return x ? 1 + bytecount(x >> 8) : 0; }
template <int N>
struct bytetype {};
template <uint64_t N>
struct minimal_uint : bytetype<bytecount(N)> {};
}  // namespace template_util
template <class T>
T next(istream& in) {
  T ret;
  in >> ret;
  return ret;
}
vec2d<int> f;
vec2d<int> ds;
vec2d<int> u;
vec2d<int> isCycle;
vec2d<int> toCycle;
vec2d<pair<int, int>> cycles;
vec<pair<int, int>> curCycle;
int n, m;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const string sdir = "URDL";
int cycleId;
void bfs(int x, int y) {
  vector<pair<int, int>> path;
  int cx = -1, cy = -1;
  int toCycleId = -1;
  while (true) {
    u[x][y] = 1;
    path.push_back({x, y});
    int nx = x + dx[ds[x][y]];
    int ny = y + dy[ds[x][y]];
    if (toCycle[nx][ny] != -1) {
      toCycleId = toCycle[nx][ny];
      break;
    }
    if (u[nx][ny]) {
      cx = nx;
      cy = ny;
      break;
    }
    x = nx;
    y = ny;
  }
  if (toCycleId != -1) {
    for (auto& p : path) {
      toCycle[p.first][p.second] = toCycleId;
    }
    return;
  }
  bool inCycle = false;
  ++cycleId;
  curCycle.clear();
  for (int i = 0; i < (path.size()); ++i) {
    if (path[i] == make_pair(cx, cy)) {
      inCycle = true;
    }
    toCycle[path[i].first][path[i].second] = cycleId;
    if (inCycle) {
      curCycle.push_back(path[i]);
      isCycle[path[i].first][path[i].second] = 1;
    }
  }
  cycles.push_back(curCycle);
}
void dfs2(int ci, int cj, int x, int y, vec2d<pair<int, int>>& mapTo) {
  for (int i = 0; i < (4); ++i) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
    if (isCycle[nx][ny]) continue;
    int nnx = nx + dx[ds[nx][ny]];
    int nny = ny + dy[ds[nx][ny]];
    if (nnx == x && nny == y) {
      mapTo[nx][ny] = cycles[ci][cj];
      dfs2(ci, (cj - 1 + cycles[ci].size()) % cycles[ci].size(), nx, ny, mapTo);
    }
  }
}
void solve(istream& in, ostream& out) {
  auto test = next<int>(in);
  while (test--) {
    cycles.clear();
    n = next<int>(in);
    m = next<int>(in);
    f = makeVec2d(n, m, 0);
    ds = makeVec2d(n, m, 0);
    for (int i = 0; i < (n); ++i) {
      auto s = next<string>(in);
      for (int j = 0; j < (m); ++j) {
        if (s[j] == '1') {
          f[i][j] = 1;
        }
      }
    }
    for (int i = 0; i < (n); ++i) {
      auto s = next<string>(in);
      for (int j = 0; j < (m); ++j) {
        auto pos = sdir.find(s[j]);
        ds[i][j] = pos;
      }
    }
    u = makeVec2d(n, m, 0);
    isCycle = makeVec2d(n, m, 0);
    toCycle = makeVec2d(n, m, -1);
    cycleId = -1;
    for (int i = 0; i < (n); ++i) {
      for (int j = 0; j < (m); ++j) {
        if (!u[i][j]) {
          bfs(i, j);
        }
      }
    }
    ++cycleId;
    vec2d<pair<int, int>> mapTo = makeVec2d<pair<int, int>>(n, m);
    for (int i = 0; i < (cycleId); ++i) {
      for (int j = 0; j < (cycles[i].size()); ++j) {
        dfs2(i, (j - 1 + cycles[i].size()) % cycles[i].size(),
             cycles[i][j].first, cycles[i][j].second, mapTo);
      }
    };
    ;
    ;
    ;
    ;
    ;
    for (int i = 0; i < (n); ++i) {
      ;
      ;
    };
    ;
    for (int i = 0; i < (n); ++i) {
      ;
      ;
    };
    ;
    for (int i = 0; i < (n); ++i) {
      ;
      ;
    }
    int res = 0, resBlack = 0;
    auto rob = makeVec2d(n, m, 0);
    for (int i = 0; i < (n); ++i) {
      for (int j = 0; j < (m); ++j) {
        if (isCycle[i][j]) {
          ++res;
          rob[i][j] = 1;
          if (!f[i][j]) {
            ++resBlack;
          }
        }
      }
    }
    for (int i = 0; i < (n); ++i) {
      for (int j = 0; j < (m); ++j) {
        if (!isCycle[i][j]) {
          int x = mapTo[i][j].first;
          int y = mapTo[i][j].second;
          if (rob[x][y]) {
            if (f[x][y] && !f[i][j]) {
              ++resBlack;
              rob[x][y] = 0;
            }
          }
        }
      }
    }
    out << res << " " << resBlack << "\n";
  }
}
int main() {
  solve(cin, cout);
  return 0;
}
