/* package workspace; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
static List<Integer> minSeen = new ArrayList<Integer>();
{
Scanner sc
= new Scanner
(System.
in); int t = sc.nextInt();
for(int i=0;i<t;i++) {
int n = sc.nextInt();
int m = sc.nextInt();
char[][] arr = new char[n][m];
sc.nextLine();
int startI = -1;
int startJ = -1;
for(int j=0;j<n;j++) {
int count=0;
while(tok.hasMoreTokens()) {
arr[j][count++] = s.charAt(0);
if(s.charAt(0) == 'B') {
startI = j;
startJ = m-1;
}
}
}
Map
<String, Boolean
> tracker
= new HashMap
<String, Boolean
>(); catchP(arr, tracker, 0, n, m, startI, startJ);
// find min over minSeen here.
if( min > minI) {
min = minI;
}
}
}
}
static boolean seenAlready
(Map
<String, Boolean
> tracker,
int n,
int m
) { return tracker.containsKey(key);
}
static boolean isValid(int row, int col, int n, int m) {
if( row >= 0 && row <= n-1 && col >=0 && col <= m-1) {
return true;
}
return false;
}
static void catchP
(char[][] arr, Map
<String, Boolean
> tracker,
int len,
int n,
int m,
int startI,
int startJ
) { Map
<String, Boolean
> currentTracker
= new HashMap
<String, Boolean
>(); if(arr[startI][startJ] == 'C') {
minSeen.add(len);
} else if ( arr[startI][startJ] == 'D' || arr[startI][startJ]=='.' && seenAlready(tracker, startI, startJ) ) {
return;
} else if ( arr[startI][startJ]=='.' && !seenAlready(tracker, startI, startJ) || arr[startI][startJ] == 'B' ) {
// copy currentTracker same as tracker.
tracker.forEach(currentTracker::putIfAbsent);
currentTracker.put(newKey, true);
if( isValid(startI, startJ-1, n, m)) {
catchP(arr, currentTracker, len+1, n, m, startI, startJ-1);
}
if( isValid(startI-1, startJ, n , m)) {
catchP(arr, currentTracker, len+1, n, m, startI-1, startJ);
}
if( isValid(startI, startJ+1, n, m) ) {
catchP(arr, currentTracker, len+1, n, m, startI, startJ+1);
}
if( isValid(startI+1, startJ, n, m) ) {
catchP(arr, currentTracker, len+1, n, m, startI+1,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TsKCQkgICAgICAgICAgICBhcnJbal1bY291bnQrK10gPSBzLmNoYXJBdCgwKTsKCQkgICAgICAgICAgICBpZihzLmNoYXJBdCgwKSA9PSAnQicpIHsKCQkgICAgICAgICAgICAgICAgc3RhcnRJID0gajsKCQkgICAgICAgICAgICAgICAgc3RhcnRKID0gbS0xOwoJCSAgICAgICAgICAgIH0KCQkgICAgICAgIH0KCQkgICAgfQoJCSAgICBNYXA8U3RyaW5nLCBCb29sZWFuPiB0cmFja2VyID0gbmV3IEhhc2hNYXA8U3RyaW5nLCBCb29sZWFuPigpOwoJCSAgICBjYXRjaFAoYXJyLCB0cmFja2VyLCAwLCBuLCBtLCBzdGFydEksIHN0YXJ0Sik7CgkJICAgIC8vIGZpbmQgbWluIG92ZXIgbWluU2VlbiBoZXJlLgoJCSAgICBpbnQgbWluID0gSW50ZWdlci5NQVhfVkFMVUU7CgkJICAgIGZvcihJbnRlZ2VyIG1pbkkgOiBtaW5TZWVuKSB7CgkJICAgICAgICBpZiggbWluID4gbWluSSkgewoJCSAgICAgICAgICAgIG1pbiA9IG1pbkk7CgkJICAgICAgICB9CgkJICAgIH0KCQkgICAgU3lzdGVtLm91dC5wcmludGxuKG1pbik7CgkJfQoJfQoJCglzdGF0aWMgYm9vbGVhbiBzZWVuQWxyZWFkeShNYXA8U3RyaW5nLCBCb29sZWFuPiB0cmFja2VyLCBpbnQgbiwgaW50IG0pIHsKCSAgICBTdHJpbmcga2V5ID0gSW50ZWdlci50b1N0cmluZyhuKSArICIuIiArIEludGVnZXIudG9TdHJpbmcobSk7CgkgICAgcmV0dXJuIHRyYWNrZXIuY29udGFpbnNLZXkoa2V5KTsKCX0KCQoJc3RhdGljIGJvb2xlYW4gaXNWYWxpZChpbnQgcm93LCBpbnQgY29sLCBpbnQgbiwgaW50IG0pIHsKCSAgICBpZiggcm93ID49IDAgJiYgcm93IDw9IG4tMSAmJiBjb2wgPj0wICYmIGNvbCA8PSBtLTEpIHsKCSAgICAgICAgcmV0dXJu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