#include <bits/stdc++.h>
using namespace std;
long long mulmod(long long, long long);
long long fastpower(long long, long long);
void getprime(vector<bool> &);
void getfact(vector<long long> &, vector<long long> &);
long long ncr(long long, long long, vector<long long> &, vector<long long> &);
bool safe(long long i, long long j, long long n, long long m) {
  if (i >= 0 && i < n && j >= 0 && j < m) return true;
  return false;
}
void DFS(int i, int j, vector<string> &A, vector<vector<bool>> &vis,
         long long &res) {
  int n = A.size(), m = A[0].size();
  if (A[i][j] == 'C') return;
  long long x = i, y = j;
  vis[i][j] = true;
  if (A[i][j] == 'R') y++;
  if (A[i][j] == 'D') x++;
  if (safe(x, y, n, m))
    DFS(x, y, A, vis, res);
  else {
    if (A[i][j] == 'R') {
      A[i][j] = 'D';
      x++;
      y--;
    } else if (A[i][j] == 'D') {
      A[i][j] = 'R';
      x--;
      y++;
    }
    res++;
    DFS(x, y, A, vis, res);
  }
}
void solve() {
  long long n, m, res = 0;
  cin >> n >> m;
  vector<string> A(n);
  for (long long i = 0; i < n; i++) cin >> A[i];
  vector<vector<bool>> vis(n, vector<bool>(m, false));
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      if (!vis[i][j]) {
        DFS(i, j, A, vis, res);
      }
    }
  }
  cout << res << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
long long mulmod(long long a, long long b) {
  long long res = 0;
  a = a % 1000000007;
  while (b > 0) {
    if (b % 2 == 1) res = (res + a) % 1000000007;
    a = (a * 2) % 1000000007;
    b /= 2;
  }
  return res % 1000000007;
}
long long fastpower(long long a, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n % 2 != 0) {
      res = (res * a) % 1000000007;
    }
    n /= 2;
    a = (a * a) % 1000000007;
  }
  return res;
}
void getprime(vector<bool> &prime) {
  prime[0] = false;
  prime[1] = false;
  for (long long i = 2; i * i <= 1000000; i++) {
    if (prime[i]) {
      for (long long j = i * i; j <= 1000000; j += i) prime[j] = false;
    }
  }
}
void getfact(vector<long long> &fact, vector<long long> &ifact) {
  fact[0] = 1;
  ifact[0] = 1;
  for (long long i = 1; i <= 1000000; i++) {
    fact[i] = (fact[i - 1] * i) % 1000000007;
    ifact[i] = (ifact[i - 1] * fastpower(i, 1000000007 - 2)) % 1000000007;
  }
}
long long ncr(long long n, long long r, vector<long long> &fact,
              vector<long long> &ifact) {
  long long res;
  res = (((fact[n] * ifact[r]) % 1000000007) * ifact[n - r]) % 1000000007;
  return res;
}
