#include <bits/stdc++.h>
using namespace std;
long long n, m;
vector<char> color;
vector<bool> visited;
vector<bool> black;
vector<vector<char>> grid;
vector<vector<long long>> nxt;
vector<vector<long long>> id;
void prep(long long i, long long j) {
  if (grid[i][j] == 'U') {
    nxt[id[i][j]][0] = id[i - 1][j];
  } else if (grid[i][j] == 'D') {
    nxt[id[i][j]][0] = id[i + 1][j];
  } else if (grid[i][j] == 'R') {
    nxt[id[i][j]][0] = id[i][j + 1];
  } else if (grid[i][j] == 'L') {
    nxt[id[i][j]][0] = id[i][j - 1];
  }
}
void reset() {
  color.clear();
  visited.clear();
  black.clear();
  grid.clear();
  nxt.clear();
  id.clear();
}
void Resize() {
  color.resize(n * m + 1);
  black.resize(n * m + 1);
  visited.resize(n * m + 1);
  grid.resize(n + 1);
  for (long long i = 0; i <= n; ++i) grid[i].resize(m + 1);
  nxt.resize(n * m + 1);
  for (long long i = 0; i <= n * m; ++i) nxt[i].resize(23);
  id.resize(n + 1);
  for (long long i = 0; i <= n; ++i) id[i].resize(m + 1);
}
void input() {
  cin >> n >> m;
  Resize();
  char c;
  for (long long i = 1; i <= n; ++i) {
    for (long long j = 1; j <= m; ++j) {
      cin >> c;
      color[m * (i - 1) + j] = c;
    }
  }
  for (long long i = 1; i <= n; ++i) {
    for (long long j = 1; j <= m; ++j) {
      cin >> grid[i][j];
    }
  }
  for (long long i = 1; i <= n; ++i) {
    for (long long j = 1; j <= m; ++j) {
      id[i][j] = m * (i - 1) + j;
    }
  }
  for (long long i = 1; i <= n; ++i) {
    for (long long j = 1; j <= m; ++j) {
      prep(i, j);
    }
  }
  for (long long deg = 1; deg <= 21; ++deg) {
    for (long long id = 1; id <= n * m; ++id) {
      if (nxt[id][deg - 1] != -1) {
        nxt[id][deg] = nxt[nxt[id][deg - 1]][deg - 1];
      }
    }
  }
}
void solve() {
  long long total = 0;
  long long cnt_black = 0;
  for (long long i = 1; i <= n * m; ++i) {
    long long j = i;
    long long rem = n * m + 1;
    for (long long l = 21; l >= 0; --l) {
      if (rem >= (1LL << l)) {
        rem -= (1LL << l);
        j = nxt[j][l];
      }
    }
    if (!visited[j]) {
      visited[j] = true;
      ++total;
      if (color[i] == '0') {
        ++cnt_black;
        black[j] = true;
      }
    } else if (visited[j] && !black[j] && color[i] == '0') {
      ++cnt_black;
      black[j] = true;
    }
  }
  cout << total << ' ' << cnt_black << '\n';
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long t = 1;
  cin >> t;
  while (t--) {
    reset();
    input();
    solve();
  }
  return 0;
}
