Language: C++

#include <bits/stdc++.h>
#define FOR(i,n) for(int i=0;i<(int)(n);i++)
#define FORR(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define pb(a) push_back(a)
#define mp(x,y) make_pair(x,y)
#define mp(x,y,z) make_tuple(x,y,z)
#define ALL(a) a.begin(),a.end()
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,-1,sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pint pair<int,int>
#define tint tuple<int,int,int>
#define INF 1<<29
#define MAX 501
using namespace std;

int R,W[2],H[2],X[2],Y[2],L[2][MAX][MAX],d[2][MAX][MAX],dd[4]={0,-1,0,1};
map<int,int> m[2];

void solve(){
 for(;;){
  cin>>R;
  if(!R) return;
  FOR(i,2){
   cin>>W[i]>>H[i]>>X[i]>>Y[i];
   X[i]--;
   Y[i]--;
   FOR(j,H[i]) FOR(k,W[i]) cin>>L[i][j][k];
   FOR(j,H[i]) FOR(k,W[i]) d[i][j][k]=INT_MAX/3;
   d[i][X[i]][Y[i]]=1;
   priority_queue<pint,vextor<pint>,greater<pint>> pque;
   que.push(mp(1,X[i]*W[i]+Y[i]));
   while(!que.empty()){
    pint p=pque.top(); pque.pop();
    int x=p.second/W[i],y=p.second-x*W[i];
    if(d[i][x][y]<p.first) continue;
    FOR(j,4){
     int nx=x+dd[j],ny=y+dd[j^1];
     if(nx<0||ny<0||nx>=H[i]||ny>=W[i]) continue;
     if(d[i][nx][ny]>max(d[i][x][y],L[i][x][y])){
      d[i][nx][ny]=max(d[i][x][y],L[i][x][y]);
      pque.push(mp(d[i][nx][ny],nx*W[i]+ny));
     }
    }
   }
   FOR(j,H[i]) FOR(k,W[i]) m[i][d[i][j][k]]++;
   for(map<int,int>::itelator ite=m[i].begin()+1;ite!=m[i].end();ite++){
    m[i][ite->first]+=m[i][(ite-1)->first];
   }
  }
  int mi=m[1].lower_bound(R)->first;
  for(map<int,int>::itelator ite=m[0].begin();ite!=m[0].end();ite++){
   mi=min(mi,(ite->first)+(m[1].lower_bound(R-(ite->second))->first));
  }
  cout<<mi<<endl;
  FOR(i,2) map[i].clear();
 }
}

int main(){
 solve();
 return 0;
}

Compiler output:
a.cc:6:9: warning: "mp" redefined
    6 | #define mp(x,y,z) make_tuple(x,y,z)
      |         ^~
a.cc:5:9: note: this is the location of the previous definition
    5 | #define mp(x,y) make_pair(x,y)
      |         ^~
a.cc:33:32: error: macro "mp" requires 3 arguments, but only 2 given
   33 |    que.push(mp(1,X[i]*W[i]+Y[i]));
      |                                ^
a.cc:6:9: note: macro "mp" defined here
    6 | #define mp(x,y,z) make_tuple(x,y,z)
      |         ^~
a.cc:43:43: error: macro "mp" requires 3 arguments, but only 2 given
   43 |       pque.push(mp(d[i][nx][ny],nx*W[i]+ny));
      |                                           ^
a.cc:6:9: note: macro "mp" defined here
    6 | #define mp(x,y,z) make_tuple(x,y,z)
      |         ^~
a.cc: In function 'void solve()':
a.cc:32:24: error: 'vextor' was not declared in this scope
   32 |    priority_queue<pint,vextor<pint>,greater<pint>> pque;
      |                        ^~~~~~
a.cc:32:35: error: template argument 2 is invalid
   32 |    priority_queue<pint,vextor<pint>,greater<pint>> pque;
      |                                   ^
a.cc:32:35: error: template argument 3 is invalid
a.cc:32:36: error: expected unqualified-id before ',' token
   32 |    priority_queue<pint,vextor<pint>,greater<pint>> pque;
      |                                    ^
a.cc:32:49: error: invalid declarator before '>' token
   32 |    priority_queue<pint,vextor<pint>,greater<pint>> pque;
      |                                                 ^~
a.cc:33:4: error: 'que' was not declared in this scope
   33 |    que.push(mp(1,X[i]*W[i]+Y[i]));
      |    ^~~
a.cc:33:13: error: 'mp' was not declared in this scope
   33 |    que.push(mp(1,X[i]*W[i]+Y[i]));
      |             ^~
a.cc:35:12: error: 'pque' was not declared in this scope
   35 |     pint p=pque.top(); pque.pop();
      |            ^~~~
a.cc:48:22: error: 'itelator' is not a member of 'std::map<int, int>'
   48 |    for(map<int,int>::itelator ite=m[i].begin()+1;ite!=m[i].end();ite++){
      |                      ^~~~~~~~
a.cc:48:50: error: 'ite' was not declared in this scope
   48 |    for(map<int,int>::itelator ite=m[i].begin()+1;ite!=m[i].end();ite++){
      |                                                  ^~~
a.cc:53:21: error: 'itelator' is not a member of 'std::map<int, int>'
   53 |   for(map<int,int>::itelator ite=m[0].begin();ite!=m[0].end();ite++){
      |                     ^~~~~~~~
a.cc:53:47: error: 'ite' was not declared in this scope; did you mean 'write'?
   53 |   for(map<int,int>::itelator ite=m[0].begin();ite!=m[0].end();ite++){
      |                                               ^~~
      |                                               write
a.cc:57:16: error: redeclaration of 'auto i'
   57 |   FOR(i,2) map[i].clear();
      |                ^
a.cc:57:7: note: 'int i' previously declared here
   57 |   FOR(i,2) map[i].clear();
      |       ^
a.cc:2:26: note: in definition of macro 'FOR'
    2 | #define FOR(i,n) for(int i=0;i<(int)(n);i++)
      |                          ^
a.cc:57:18: error: expected initializer before '.' token
   57 |   FOR(i,2) map[i].clear();
      |                  ^
a.cc:57:18: error: expected ';' before '.' token