#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "iomanip"
#include "cmath"

using namespace std;

const long long int MOD = 1000000007;

long long int N, M, K, H, W, L, R;

class CBox {
public:
	int c;
	int x;
	int y;
	CBox(){
		c = INT_MAX;
	}
	bool operator < (const CBox& b)const {
		return c < b.c;
	}
	bool operator > (const CBox& b)const {
		return c > b.c;
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> K;
	while (K) {
		int h1, h2, w1, w2, sx1, sy1, sx2, sy2;
		cin >> w1 >> h1 >> sx1 >> sy1;
		sx1--;
		sy1--;
		vector<vector<int>>field1(h1, vector<int>(w1));
		for (int i = 0; i < h1; i++) {
			for (int j = 0; j < w1; j++) {
				cin >> field1[i][j];
			}
		}
		cin >> w2 >> h2 >> sx2 >> sy2;
		sx2--;
		sy2--;
		vector<vector<int>>field2(h2, vector<int>(w2));
		for (int i = 0; i < h2; i++) {
			for (int j = 0; j < w2; j++) {
				cin >> field2[i][j];
			}
		}
		vector<vector<CBox>>min1(h1, vector<CBox>(w1));
		min1[sy1][sx1].c = 1;
		for (int i = 0; i < h1; i++) {
			for (int j = 0; j < w1; j++) {
				min1[i][j].y = i;
				min1[i][j].x = j;
			}
		}
		priority_queue<CBox,vector<CBox>,greater<CBox>>PQ;
		PQ.push(min1[sy1][sx1]);
		int dir[] = { 1,0,-1,0,1 };
		while (!PQ.empty()) {
			int cx = PQ.top().x;
			int cy = PQ.top().y;
			int c = PQ.top().c;
			PQ.pop();
			if (min1[cy][cx].c < c) {
				continue;
			}
			for (int i = 0; i < 4; i++) {
				int nx = cx + dir[i + 1];
				int ny = cy + dir[i];
				if (ny < 0 || ny >= h1 || nx < 0 || nx >= w1) {
					continue;
				}
				if (min1[ny][nx].c > max(c, field1[ny][nx])) {
					min1[ny][nx].c = max(c, field1[ny][nx]);
					PQ.push(min1[ny][nx]);
				}
			}
		}
		vector<vector<CBox>>min2(h2, vector<CBox>(w2));
		min2[sy2][sx2].c = 1;
		for (int i = 0; i < h2; i++) {
			for (int j = 0; j < w2; j++) {
				min2[i][j].y = i;
				min2[i][j].x = j;
			}
		}
		PQ.push(min2[sy2][sx2]);
		while (!PQ.empty()) {
			int cx = PQ.top().x;
			int cy = PQ.top().y;
			int c = PQ.top().c;
			PQ.pop();
			if (min2[cy][cx].c < c) {
				continue;
			}
			for (int i = 0; i < 4; i++) {
				int nx = cx + dir[i + 1];
				int ny = cy + dir[i];
				if (ny < 0 || ny >= h2 || nx < 0 || nx >= w2) {
					continue;
				}
				if (min2[ny][nx].c > max(c, field2[ny][nx])) {
					min2[ny][nx].c = max(c, field2[ny][nx]);
					PQ.push(min2[ny][nx]);
				}
			}
		}
		vector<int>v1;
		for (int i = 0; i < h1; i++) {
			for (int j = 0; j < w1; j++) {
				v1.push_back(min1[i][j].c);
			}
		}
		vector<int>v2;
		for (int i = 0; i < h2; i++) {
			for (int j = 0; j < w2; j++) {
				v2.push_back(min2[i][j].c);
			}
		}
		v1.push_back(0);
		v2.push_back(0);
		sort(v1.begin(), v1.end());
		sort(v2.begin(), v2.end());
		N = v1.size()-1;
		M = v2.size()-1;
		L = 0;
		R = 100000001;
		//for (auto i : v2) {
		//	cout << i << endl;
		//}
		while (R - L > 1) {
			int mid = (R + L + 1) / 2;
		//	cout << "  " << mid << endl;
			bool flag = false;
			for (int i = 0; i <= N; i++) {
				int bag = K - i;
				if (bag < 0) {
					bag = 0;
				}
				if (bag > M) {
					continue;
				}
			//	cout << "   " << v1[i] + v2[bag] << endl;
				if (v1[i] + v2[bag] <= mid) {
					flag = true;
				}
			}
			if (!flag) {
				L = mid;
			}
			else {
				R = mid;
			}
		}
		cout << R << endl;
		cin >> K;
	}
	return 0;
}

Measured memory_bytes: 13392