#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
string col[maxn], buf[maxn];
vector<vector<int>> vis, dp, black, white;
int n, m;
void getNext(int x, int y, int& sx, int& sy) {
  if (buf[x][y] == 'L') sx = x, sy = y - 1;
  if (buf[x][y] == 'R') sx = x, sy = y + 1;
  if (buf[x][y] == 'U') sx = x - 1, sy = y;
  if (buf[x][y] == 'D') sx = x + 1, sy = y;
}
void dfs(int x, int y) {
  int sx, sy;
  getNext(x, y, sx, sy);
  int now = x * m + y;
  int Next = sx * m + sy;
  dp[now][0] = Next;
  vis[x][y] = 1;
  if (!vis[sx][sy]) dfs(sx, sy);
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    black = white = vis = vector<vector<int>>(n, vector<int>(m, 0));
    dp = vector<vector<int>>(n * m, vector<int>(22, 0));
    for (int i = 0; i < n; i++) {
      cin >> col[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> buf[i];
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (!vis[i][j]) dfs(i, j);
      }
    }
    for (int d = 1; d < 21; d++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          int now = i * m + j;
          dp[now][d] = dp[dp[now][d - 1]][d - 1];
        }
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int sum = n * m;
        int u = i * m + j;
        for (int k = 0; k < 21; k++) {
          if ((sum >> k) & 1) u = dp[u][k];
        }
        int x = u / m, y = u % m;
        if (col[i][j] == '0')
          black[x][y] = 1;
        else
          white[x][y] = 1;
      }
    }
    int ans1 = 0, ans2 = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (black[i][j])
          ans1++, ans2++;
        else if (white[i][j])
          ans1++;
      }
    }
    cout << ans1 << " " << ans2 << endl;
  }
  return 0;
}
