// Arup Guha
// 6/18/2015
// Solution to SI@UCF Competiton Camp Contest #3 Problem E: Jumpman

import java.util.*;

public class jumpman_arup {

	// Valid jumping movements.
	public final static int[] DX = {-1,0,0,1};
	public final static int[] DY = {0,-1,1,0};

	// Common elements for each case.
	public static int r;
	public static int c;
	public static int maxJump;
	public static int[][] elevation;
	public static int[][] points;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();

		// Process all cases.
		for (int loop=0; loop<numCases; loop++) {

			// Get dimensions and jump.
			r = stdin.nextInt();
			c = stdin.nextInt();
			maxJump = stdin.nextInt();

			// Starting position.
			int x = stdin.nextInt()-1;
			int y = stdin.nextInt()-1;

			// Elevations.
			elevation = new int[r][c];
			for (int i=0; i<r; i++)
				for (int j=0; j<c; j++)
					elevation[i][j] = stdin.nextInt();

			// Points
			points = new int[r][c];
			for (int i=0; i<r; i++)
				for (int j=0; j<c; j++)
					points[i][j] = stdin.nextInt();

			// Run and output DFS result.
			boolean[][] visited = new boolean[r][c];
			System.out.println(go(x,y, visited));
		}
	}

	// Runs DFS from row x, column y.
	public static int go(int x, int y, boolean[][] visited) {

		// Mark visiting this square and add the points.
		int pts = points[x][y];
		visited[x][y] = true;

		// Go all four ways and add up the scores.
		for (int i=0; i<DX.length; i++)
			if (inbounds(x+DX[i], y+DY[i]) && !visited[x+DX[i]][y+DY[i]] && elevation[x+DX[i]][y+DY[i]] - elevation[x][y] <= maxJump)
				pts += go(x+DX[i], y+DY[i], visited);
		return pts;
	}

	// Return true iff (x,y) is inbounds.
	public static boolean inbounds(int x, int y) {
		return x >= 0 && x < r && y >= 0 && y < c;
	}
}