#include <bits/stdc++.h>
#define ll int
#define pll pair<ll,ll>
#define db long double
#define foru(i,a,b) for(ll i=(a);i<=(b);i++)
#define ford(i,a,b) for(ll i=(a);i>=(b);i--)
#define ALL(a) (a).begin(),(a).end()
#define ROUND(i) fixed<<setprecision(i)
#define fi first
#define se second
using namespace std;
int NumTest=1,CurTest;
clock_t start_time_=clock();
namespace fasstIO {
    const int SZ = 1 << 20;
    char in[SZ], out[SZ];
    int ip = 0, len = 0, op = 0;

    inline char gc() {
        if (ip == len) {
            len = fread(in, 1, SZ, stdin);
            ip = 0;
            if (!len) return 0;
        }
        return in[ip++];
    }

    inline int readInt() {
        int x = 0, sign = 1;
        char c = gc();
        while (c <= ' ' && c) c = gc();
        if (c == '-') sign = -1, c = gc();
        while (c >= '0' && c <= '9') {
            x = x * 10 + c - '0';
            c = gc();
        }
        return x * sign;
    }

    inline void flush() {
        fwrite(out, 1, op, stdout);
        op = 0;
    }

    inline void writeInt(int x, char end = '\n') {
        if (x < 0) out[op++] = '-', x = -x;
        char s[12];
        int n = 0;
        do s[n++] = x % 10 + '0', x /= 10; while (x);
        while (n--) {
            if (op == SZ) flush();
            out[op++] = s[n];
        }
        if (op == SZ) flush();
        out[op++] = end;
    }

    struct AutoFlush {
        ~AutoFlush() { flush(); }
    } autoFlush;
}
///⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
///⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣠⣶⣾⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
///⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⡞⠀⠀⢀⣤⣴⣾⣿⣿⣿⣿⣿⣿⣿⠇⠀⠀⠀⣀⠀⠀⠀⠀⠀⠀⠀
///⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣶⣿⣿⣣⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣤⣶⣾⣿⠃⠀⠀⠀⠀⠀⠀⠀
///⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠁⠀⣀⣀⣀⡤⠀⠀⠀
///⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⠀⠀
///⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀
///⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⠀⠀⠀⠀
///⠀⠀⠀⠀⠀⠀⠀⣠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣤⣶⣾⡟⠀
///⠀⠀⠀⠀⠀⠀⡰⢹⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠁⠀
///⠀⠀⠀⠀⠀⢠⣳⣿⣾⣿⣿⣿⣿⢋⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⣷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⠀
///⠀⠀⠀⠀⠀⣸⣿⣿⣿⣿⣿⣿⣿⡞⠀⠈⠙⠿⠿⠟⠛⠉⠀⠀⢹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠀⠀⠀
///⠀⠀⠀⠀⢀⣿⣿⣿⣿⣿⣿⣿⣿⠁⠀⠀⡄⠀⠀⠀⠀⠀⠀⠐⡄⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠁⠀⠀⠀
///⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⢿⣿⣿⠀⠀⡌ -TFA-⠀⠀ ⠀⢻⣿⣿⡇⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⣴⡶⠋⠁
///⠀⠀⠀⠀⢸⣿⣿⣿⣿⡟⢻⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣀⣼⣿⣿⠗⢺⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠀⠀⠀
///⠀⠀⠀⠀⢸⣿⣿⣿⣿⣇⣀⣻⣇⠈⣖⣄⠀⠀⠀⢀⡤⠖⣻⢍⣁⣀⣿⣏⡀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠋⠀⠀⠀⠀
///⠀⠀⠀⠀⢸⡿⣿⣿⣿⡇⢻⢻⣿⣿⣶⡽⠂⠀⠀⠀⠀⢽⣵⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣵⣶⠖⠋⠀⠀
///⠀⠀⠀⠀⢸⡇⢸⣿⣿⣿⡈⠘⢛⣿⢿⡇⠀⠀⠀⠀⠀⠈⠡⠼⠏⣻⣿⡿⢻⠏⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠁⠀⠀⠀⠀
///⠀⠀⠀⠀⢸⠁⠈⣿⣿⣿⡇⠈⠪⠷⠚⠁⠀⠀⠀⠀⠀⠀⠀⠀⠈⠽⠷⠞⠃⠀⢸⣿⣿⣿⣿⢏⣿⣿⣿⡟⠁⠀⠀⠀⠀⠀⠀
///⠀⠀⠀⠀⠀⠀⠀⢹⣿⣿⣷⠀⠀⠀⠀⠀⠀⢀⢠⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⢸⣿⣿⣿⣏⣡⣿⣿⣿⣇⠀⠀⠀⠀⠀⠀⠀
///⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣧⡀⠀⠀⠀⠀⠀⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⡰⠃⢸⣿⣿⣿⣿⣿⡟⠀⠉⠛⠣⠀⠀⠀⠀⠀⠀
///⠀⠀⠀⠀⠀⠀⠀⠀⠘⣿⣿⡟⠙⠢⣄⠀⠀⠀⠒⢂⠠⠀⠀⠀⠀⠀⠀⢈⡠⠔⢻⣿⣿⡟⣿⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
///⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⡿⠇⠀⠀⡨⠋⢲⠤⣀⠈⠀⢀⣀⡀⠤⢖⠞⠉⠀⠀⢸⡟⠻⣄⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
///⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⡇⠀⡠⠊⠐⡄⠀⠀⡀⠉⠉⠉⠀⠀⢀⠎⠀⠀⠀⠀⠟⠀⠀⠈⢢⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
///⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡁⠀⠀⠁⣆⠏⠈⢓⠒⠋⠁⣄⠞⠀⠀⠀⠀⠀⠀⠐⢈⡤⠒⠛⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
///⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡰⡻⣠⠀⠀⠀⡿⠀⠀⠀⠀⠀⠀⡜⠀⠀⠀⠀⠀⠀⠀⡴⠋⠀⠳⡀⠸⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀
///⠀⠀⠀⠀⠀⠀⠀⠀⠀⡜⠁⠀⡟⠀⠀⠀⡇⠀⠀⠀⠀⠀⢰⠁⠀⠀⠀⠀⠀⢀⠜⠀⠀⠀⠀⠀⠀⢳⠀⠀⠀⠀⠀⠀⠀⠀⠀
///⠀⠀⠀⠀⠀⠀⣀⠠⠞⠀⠀⢀⠇⠀⠀⠀⡇⠀⠀⠀⠀⢀⡆⠀⠀⠀⠀⠀⠀⡌⠀⢀⠂⠀⠀⠀⠀⠈⡄⠀⠀⠀⠀⠀⠀⠀⠀
///⠀⠀⠀⣠⠔⠊⠁⠀⠀⠀⠀⢸⠀⠀⠀⠀⡇⠀⠀⠀⠀⡜⠀⠀⠀⠀⠀⠀⠀⡇⠀⡜⠀⠀⠀⠀⠀⠀⢹⡀⠀⠀⠀⠀⠀⠀⠀
///⣠⣖⠉⠁⠀⠀⠀⠀⠀⠀⠀⡄⠀⠀⠀⠀⢰⣀⡀⠀⡼⠀⠀⠀⠒⠀⠀⠠⠂⠇⢠⠁⠀⠀⠀⠀⠀⠀⠀⢇⠀⠀⠀⠀⠀⠀⠀
///⠸⣟⣿⡇⠀⠀⠀⠀⠀⠀⢠⣷⣖⡒⣆⠀⢲⣷⣉⠞⣠⠖⠒⠒⠲⢤⣤⣀⣀⣰⡜⠀⠀⠀⠀⠀⠀⠀⠀⠸⡄⠀⠀⠀⠀⠀⠀
///⠀⠀⠈⢹⡀⠀⠀⠀⠀⠀⠀⢹⣽⣒⣼⣿⣿⣿⣧⡞⠓⠒⠀⢀⣴⣯⣾⣷⡘⢻⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⢣⠀⠀
const ll dx[]={+1,0,-1,0};
const ll dy[]={0,-1,0,+1};
ll n,m,q;
bool g[505][505];
pll opt[2500005];
ll par[2500005];
ll last[505][505];
ll id[505][505],k=0;
ll headq[2500005],nxtq[2500005],arrq[2500005],szq=0;
ll headc[2500005],nxtc[2500005],arrc[2500005],szc=0;
ll finish[20005];
ll result=0;

bool valid(ll x,ll y){
	return 1<=x && x<=n && 1<=y && y<=m && g[x][y]==1;
}
void prebuild(){
	memset(last,0x3f,sizeof(last));
	ford(i,q,1){
		auto [x,y]=opt[i];
		par[i]=q+1;
		foru(e,0,3){
			ll nx=x+dx[e], ny=y+dy[e];
		
			if(valid(nx,ny)){
				par[i]=min(par[i],last[nx][ny]);
			}
		}
		arrc[++szc]=i; nxtc[szc]=headc[par[i]]; headc[par[i]]=szc;
		last[x][y]=i;
	}	

	foru(i,1,n)foru(j,1,m)if(g[i][j]==1){
		id[i][j]=++k;
		ll to=q+1;
		foru(e,0,3){
			ll nx=i+dx[e], ny=j+dy[e];
			if(valid(nx,ny))to=min(to,last[nx][ny]);
		}
		arrq[++szq]=k; nxtq[szq]=headq[to]; headq[to]=szq;
	}


}

void DFS(ll u){
	stack<pll>stk;
	stk.push({u,-1});

	while(stk.size()){
		auto[u,t]=stk.top(); stk.pop();

		if(t!=-1){
			finish[u]=t;
			continue;
		}

		ll p=id[opt[u].fi][opt[u].se];
		ll old=finish[p];
		finish[p]=u;

		int j=headq[u];
		while(j){
			int p=arrq[j]; j=nxtq[j];
			foru(x,1,k)if(x!=p)result=max(result,finish[x]);
		}

		stk.push({p,old});

		int i=headc[u];
		while(i){
			ll v=arrc[i]; i=nxtc[i];
			stk.push({v,-1});
		}

	}


}


void Uchiha_Sasuke(){
	n=fasstIO::readInt();
	m=fasstIO::readInt();
	foru(i,1,n)foru(j,1,m)g[i][j]=fasstIO::readInt();
	q=fasstIO::readInt();
	foru(i,1,q) {
	    opt[i].fi=fasstIO::readInt();
	    opt[i].se=fasstIO::readInt();
	}

	prebuild();
	DFS(q+1);

	cout<<result<<'\n';
}

signed main(){
    // ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    if(fopen("task.inp","r"))
    freopen("task.inp","r",stdin),
    freopen("task.out","w",stdout);
    srand(time(0));

    //cin>>NumTest;
    for(CurTest=1;CurTest<=NumTest;CurTest++)Uchiha_Sasuke();


    clock_t finish_time_=clock();
    double run_time_=((double)finish_time_-(double)start_time_)/CLOCKS_PER_SEC;
    cerr<<"\n\n------------------------------------------------\nRun Time : "<<fixed<<setprecision(3)<<run_time_<<"s\n\n";

    return 0;
}
