


The first line of input contains an integer ‘T’, denoting the number of test cases.
The first line of each test case contains two spaced integers, ‘N’ and ‘M’, denoting the number of rows and columns.
The following line ‘N’ lines contain a string of size ‘M’ denoting the grid where your friends live.
For each test case, print a single integer denoting the minimum sum of distance your friends need to travel or -1 if there is no meeting spot.
You are not required to print the expected output. It has already been taken care of. Just implement the function.
1 <= T <= 10
2 <= N, M <= 30
Time Limit: 1 sec
We need to find a spot with the following two properties :
We know, using BFS, we can find the shortest distance of all the spots from a single source. So we can treat each friend as a source and run a BFS to find the shortest distance of all the sports from that friend.
Now, we will maintain two arrays for each spot, denoting the number of friends that can reach that spot and the sum of distance travelled by each friend.
In the end, we need to find a spot where the count of friends that can reach that spot is equal to the total number of friends and store the minimum sum of distance travelled.