/* 
JAVA program for JONES 
Summary: Constructs a table for the state of each stone and apply dynamic
programming, complexity is O(st)
*/

import java.util.*;

class JONES {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int numCases = sc.nextInt();
        assert numCases > 0;
        while (numCases-- > 0) {
            JONESs.solve(sc);
        }
        assert sc.nextInt() == 0;
    }
}

class JONESs {
    
    //B[i][j] = behaviour of stone j at time i
    static char[][] B = new char[505][505];     
    
    //S[i][j] = state of stone j at time i
    static boolean[][] S = new boolean[505][505];

    //C[i][j] = true if Jone can reach stone j at time i
    static boolean[][] C = new boolean[505][505];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        solve(sc);
    }

    static void solve(Scanner sc) {
        int s = sc.nextInt();
        int t = sc.nextInt();

        assert s >= 1 && s <= 500;
        assert t >= 1 && t <= 500;

        for (int i = 0; i < t; i++) {
            for (int j = 0; j < s; j++) {
                B[i][j] = sc.next().charAt(0);
                assert B[i][j] == 'u' || B[i][j] == 's' || B[i][j] == 'r';
            }
        }

        //determine state from behaviour
        for (int j = 0; j < s; j++) {
            boolean isOn = true;
            S[0][j] = true;
            for (int i = 0; i < t; i++) {
                switch (B[i][j]) {
                    case 's': {
                        if (!isOn) {
                            System.out.println("Error in input, down down");
                            System.exit(1);
                        } else {
                            isOn = false; 
                        }
                        break;
                    }                        
  
                    case 'r': {
                        if (isOn) {
                            System.out.println("Error in input, up up");
                            System.exit(1);
                        } else {
                            isOn = true; 
                        }
                        break;                        
                    }
                        
                    case 'u': break;
                }
                S[i+1][j] = isOn;
            }
        }
        
        //dynamic programming to compute shortest time to reach
        
        //initialization (can always reach stone[0] if its up
        for (int i = 0; i <= t; i++) {
            C[i][0] = S[i][0];
        }

        //compute entries in increasing time
        for (int i = 1; i <= t; i++) {
            for (int j = 1; j < s; j++) {
                boolean fromRight = (j == s - 1) ? false : C[i - 1][j + 1] && S[i][j+1];
                boolean fromLeft = C[i - 1][j - 1] && S[i][j - 1];
                C[i][j] = S[i][j] && (
                    C[i - 1][j]   //stay on the same stone
                || fromLeft     //jump from left stone
                || fromRight); //jump from right stone
            }
        }

        //find smallest time t for which C[t - 1][s - 1] is true
        int minT = -1;
        for (int i = 0; i <= t - 1; i++) {
            if (C[i][s - 1] && S[i + 1][s - 1]) {
                minT = i + 1;
                break;
            }
        }
        
        System.out.println(minT);
    }
}
