#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using ui = unsigned int;
template <class T>
bool uin(T& a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T& a, T b) {
  return a < b ? (a = b, true) : false;
}
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
const ll infll = 2 * (ll)(1e18);
const int inf = int(2e9) + 10;
const ll mod = int(1e9) + 7;
const char* dirs = "UDLR";
const int di[] = {-1, 1, 0, 0};
const int dj[] = {0, 0, -1, 1};
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<vector<char>> color(n, vector<char>(m));
    vector<vector<char>> go(n, vector<char>(m));
    vector<vector<int>> state(n, vector<int>(m));
    vector<vector<int>> dist_to_cycle(n, vector<int>(m));
    vector<vector<int>> start(n, vector<int>(m, -1));
    vector<int> start_to_use(n * m);
    for (int i = 0; i < (int)(n); i++) {
      for (int j = 0; j < (int)(m); j++) {
        cin >> color[i][j];
      }
    }
    for (int i = 0; i < (int)(n); i++) {
      for (int j = 0; j < (int)(m); j++) {
        cin >> go[i][j];
      }
    }
    auto move = [&](int& ci, int& cj) {
      int dir = strchr(dirs, go[ci][cj]) - dirs;
      ci += di[dir], cj += dj[dir];
    };
    for (int i = 0; i < (int)(n); i++) {
      for (int j = 0; j < (int)(m); j++) {
        int ci = i, cj = j;
        vector<pair<int, int>> done;
        while (state[ci][cj] == 0) {
          state[ci][cj] = 1;
          done.emplace_back(ci, cj);
          move(ci, cj);
        }
        if (state[ci][cj] == 1) {
          int len = 0;
          while (state[ci][cj] == 1) {
            state[ci][cj] = 2;
            ++len;
            move(ci, cj);
          }
          int num = ci * m + cj;
          start[ci][cj] = num;
          start_to_use[num] = len;
        }
        for (int tmp = 0; tmp < done.size(); ++tmp) {
          state[done[tmp].first][done[tmp].second] = 2;
        }
      }
    }
    for (int i = 0; i < (int)(n); i++) {
      for (int j = 0; j < (int)(m); j++) {
        int ci = i, cj = j;
        vector<pair<int, int>> done;
        while (start[ci][cj] == -1) {
          done.emplace_back(ci, cj);
          move(ci, cj);
        }
        for (int tmp = 0; tmp < done.size(); ++tmp) {
          start[done[tmp].first][done[tmp].second] = start[ci][cj];
          dist_to_cycle[done[tmp].first][done[tmp].second] =
              dist_to_cycle[ci][cj] + done.size() - tmp;
        }
      }
    }
    int ans = accumulate(start_to_use.begin(), start_to_use.end(), 0);
    int ans2 = 0;
    vector<set<int>> dist(n * m);
    for (int i = 0; i < (int)(n); i++) {
      for (int j = 0; j < (int)(m); j++) {
        if (color[i][j] == '0') {
          int cell = start[i][j];
          dist[cell].insert(dist_to_cycle[i][j] % start_to_use[cell]);
        }
      }
    }
    vector<int> was(n * m);
    for (int i = 0; i < (int)(n); i++) {
      for (int j = 0; j < (int)(m); j++) {
        if (color[i][j] == '0') {
          int cell = start[i][j];
          if (!was[cell]) {
            ans2 += dist[cell].size();
          }
          was[cell] = 1;
        }
      }
    }
    cout << ans << " " << ans2 << "\n";
  }
  return 0;
}
