#include <bits/stdc++.h>
using namespace std;
int converter(int i, int j, int m) { return (i * m) + j; }
pair<int, int> reverseconverter(int x, int m) { return {x / m, x % m}; }
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m, i, j;
    cin >> n >> m;
    char c[n][m];
    for (i = 0; i < n; i++)
      for (j = 0; j < m; j++) cin >> c[i][j];
    int deg = 0;
    while ((1 << deg) < n * m) deg++;
    deg++;
    int nxt[n * m][deg];
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        char y;
        cin >> y;
        if (y == 'U')
          nxt[converter(i, j, m)][0] = converter(i - 1, j, m);
        else if (y == 'D')
          nxt[converter(i, j, m)][0] = converter(i + 1, j, m);
        else if (y == 'L')
          nxt[converter(i, j, m)][0] = converter(i, j - 1, m);
        else
          nxt[converter(i, j, m)][0] = converter(i, j + 1, m);
      }
    }
    for (i = 1; i < deg; i++) {
      for (j = 0; j < n * m; j++) {
        nxt[j][i] = nxt[nxt[j][i - 1]][i - 1];
      }
    }
    map<long long int, long long int> mp;
    long long int ans = 0, blacks = 0;
    for (i = 0; i < n * m; i++) {
      int xx;
      pair<int, int> p = reverseconverter(i, m);
      if (c[p.first][p.second] == '0')
        xx = 2;
      else
        xx = 1;
      mp[nxt[i][deg - 1]] |= xx;
    }
    map<long long int, long long int>::iterator it;
    for (it = mp.begin(); it != mp.end(); it++) {
      ans++;
      if (it->second > 1) blacks++;
    }
    cout << ans << " " << blacks << "\n";
  }
}
