#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
int n, m, col[N], nxt[N][22], ans[N], vis[N];
int get(int i, int j) { return (i - 1) * m + j; }
int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int test;
  cin >> test;
  while (test--) {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
      string str;
      cin >> str;
      for (int j = 1; j <= m; j++) {
        col[get(i, j)] = str[j - 1] - '0';
      }
    }
    for (int i = 1; i <= n; i++) {
      string str;
      cin >> str;
      for (int j = 1; j <= m; j++) {
        char ch = str[j - 1];
        if (ch == 'L') nxt[get(i, j)][0] = get(i, j - 1);
        if (ch == 'R') nxt[get(i, j)][0] = get(i, j + 1);
        if (ch == 'U') nxt[get(i, j)][0] = get(i - 1, j);
        if (ch == 'D') nxt[get(i, j)][0] = get(i + 1, j);
      }
    }
    for (int i = 1; i <= 21; i++) {
      for (int j = 1; j <= n * m; j++) {
        nxt[j][i] = nxt[nxt[j][i - 1]][i - 1];
      }
    }
    for (int i = 1; i <= n * m; i++) {
      ans[nxt[i][21]] |= !col[i];
      vis[nxt[i][21]] = 1;
    }
    int a = 0, b = 0;
    for (int i = 1; i <= n * m; i++) {
      a += vis[i];
      b += ans[i];
    }
    cout << a << " " << b << endl;
    for (int i = 1; i <= n * m; i++) {
      vis[i] = 0;
      ans[i] = 0;
    }
  }
}
