// Arup Guha
// 3/8/2016
// Solution to 2016 UCF HS Contest Problem: Corn Maze

import java.util.*;
import java.io.*;

public class maze {

	// index = 0 array if facing right, index = 1 array if facing up,
	// index = 2 array if facing left, index = 3 array if facing down.
	final public static int[][] DX = {  {1,0,-1,0}, {0,-1,0,1}, {-1,0,1,0}, {0,1,0,-1} };
	final public static int[][] DY = {  {0,1,0,-1}, {1,0,-1,0}, {0,-1,0,1}, {-1,0,1,0} };

	// Time to move one step.
	final public static int TIME_STEP = 5;
	final public static int NO_SOLUTION = -1;

	public static char[][] maze;
	public static int r;
	public static int c;
	public static HashSet<Integer> used;

	public static void main(String[] args) throws Exception {

		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		int numCases = Integer.parseInt(stdin.readLine().trim());

		// Go through each maze.
		for (int loop=1; loop<=numCases; loop++) {

			// Read in the maze.
			StringTokenizer tok = new StringTokenizer(stdin.readLine());
			r = Integer.parseInt(tok.nextToken());
			c = Integer.parseInt(tok.nextToken());
			maze = new char[r][];
			for (int i=0; i<r; i++)
				maze[i] = stdin.readLine().trim().toCharArray();
			used = new HashSet<Integer>();

			// Solve and output result.
			int res = solve();
			if (res != NO_SOLUTION)
				System.out.println("Maze #"+loop+": "+(TIME_STEP*res)+" seconds");
			else
				System.out.println("Maze #"+loop+": Impossible");
		}
	}

	public static int solve() {

		// So we know where we are starting and ending.
		int start = find('S');
		int cur = start;
		int curDir = getDir(cur);
		int end = find('E');
		int steps = 0;

		// Simulate
		while (true) {

			// Move one step.
			int next = getNextLoc(cur, curDir);
			int nextDir = getDir(cur, next);
			steps++;

			// Here are the conditions under which we stop our simulation.
			if (next == end) return steps;
			if (next == start) return NO_SOLUTION;

			// This one is tricky - if we visit the same square in the same direction twice, we're doomed!
			if (used.contains((next<<2)+nextDir)) return NO_SOLUTION;

			// Update current location and add where we've been to our used list.
			cur = next;
			curDir = nextDir;
			used.add((next<<2)+nextDir);
		}
	}

	// Return the location of the first occurrence of c, or NO_SOLUTION if there is none.
	public static int find(char ch) {

		// Just look everywhere...
		for (int i=0; i<r; i++)
			for (int j=0; j<c; j++)
				if (maze[i][j] == ch)
					return c*i+j;

		// Should never get here for our specific problem.
		return NO_SOLUTION;
	}

	public static int getDir(int loc) {
		int row = loc/c;
		int col = loc%c;

		// Directions are: 0 = right, 1 = up, 2 = left, 3 = down.
		if (col == 0) return 0;
		if (row == r-1) return 1;
		if (col == c-1) return 2;
		return 3;
	}

	public static int getNextLoc(int cur, int curDir) {

		// Extract coordinates.
		int curX = cur/c;
		int curY = cur%c;

		// Try each of the four directions, in the order specified.
		for (int i=0; i<DX[curDir].length; i++) {

			// Look at the next spot.
			int nextX = curX + DX[curDir][i];
			int nextY = curY + DY[curDir][i];

			// A move is valid if it's inbounds and not on an illegal square.
			if (inbounds(nextX, nextY) && maze[nextX][nextY] != '#')
				return nextX*c + nextY;
		}

		// Oops, no move worked!
		return NO_SOLUTION;
	}

	public static int getDir(int cur, int next) {

		// Get direction of movement.
		int mydx = next/c - cur/c;
		int mydy = next%c - cur%c;

		// Just a look up chart.
		if (mydx == 0 && mydy == 1) return 0;
		if (mydx == -1 && mydy == 0) return 1;
		if (mydx == 0 && mydy == -1) return 2;
		return 3;
	}

	public static boolean inbounds(int x, int y) {
		return x >= 0 && x < r && y >=0 && y < c;
	}

}