public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int cases = Integer.parseInt(sc.nextLine());
		for(int i = 0; i<cases; i++) {
			int width, height, vel, stop;
			int[] pj = new int[2];
			StringTokenizer st = new StringTokenizer(sc.nextLine());
			width = Integer.parseInt(st.nextToken());
			height = Integer.parseInt(st.nextToken());
			vel = Integer.parseInt(st.nextToken());
			stop = Integer.parseInt(st.nextToken());
			char[][] cave = new char[height][width];
			for(int j = 0; j<height; j++) {
				String line = sc.nextLine();
				for(int k = 0; k<width; k++) {
					if(line.charAt(k) == 'X') {
						pj[0]=j;
						pj[1]=k;
						cave[j][k] = 'V';
					}
					else {
						cave[j][k] = line.charAt(k);
					}	
				}
			}
			System.out.println(Math.round(solve(cave, vel, stop, pj, (float)Integer.MAX_VALUE, 0)));
		}
		sc.close();
	}