public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String line = "";
		StringBuilder out = new StringBuilder();
		while ((line = in.readLine()) != null && line.length() != 0) {
			int nSet = Integer.parseInt(line.trim());
			for (int i = 0; i < nSet; i++) {
				if (i == 0)
					in.readLine();
				else
					out.append("\n");
				int[] t = readInts(in.readLine());
				ArrayList<String> l = new ArrayList<String>(100);
				while ((line = in.readLine()) != null
						&& line.matches(".*[10].*"))
					l.add(line.trim());
				n = l.size();
				m = l.get(0).length();
				map = new char[n][m];
				for (int j = 0; j < l.size(); j++)
					map[j] = l.get(j).toCharArray();
				if (map[t[0] - 1][t[1] - 1] == '1')
					out.append("0\n");
				else {
					used = new boolean[n][m];
					used[t[0] - 1][t[1] - 1] = true;
					out.append(floodFill(t[0] - 1, t[1] - 1) + "\n");
				}
			}
		}
		System.out.print(out);
	}