#include <bits/stdc++.h>
#define R 3000
#define C 3000
int point[3000][3000];
int points[3000][3000];
int dp[3000][3000];
int kk[1000000][2];
using namespace std;

int minInitialPoints(int points[3000][3000],int m, int n)
{





    dp[m - 1][n - 1] = points[m - 1][n - 1] > 0
                           ? 1
                           : abs(points[m - 1][n - 1]) + 1;


    for (int i = m - 2; i >= 0; i--)
        dp[i][n - 1]
            = max(dp[i + 1][n - 1] - points[i][n - 1], 1);
    for (int j = n - 2; j >= 0; j--)
        dp[m - 1][j]
            = max(dp[m - 1][j + 1] - points[m - 1][j], 1);


    for (int i = m - 2; i >= 0; i--) {
        for (int j = n - 2; j >= 0; j--) {
            int min_points_on_exit
                = min(dp[i + 1][j], dp[i][j + 1]);
            dp[i][j]
                = max(min_points_on_exit - points[i][j], 1);
        }
    }

    return dp[0][0];
}


int main()
{


    int n,m,k,r,p;
    cin>>n>>m;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>point[i][j];
           point[i][j]=0-point[i][j];
        }
    }
cin>>k;
for(int i=0;i<k;i++){
cin>>kk[i][0]>>kk[i][1];
    }
    for(int e=0;e<k;e++){

    for(int t=0;t<n;t++){
        for(int j=0;j<m;j++){
           points[t][j]=point[t][j];
        }
    }

    r=kk[e][0]-1;
    p=kk[e][1]-1;

    points[r][p]=-2000000000;
    cout << minInitialPoints(points,n,m)-1<<endl;


}
return 0;
}