#include<iostream>
using namespace std;
void jeftinPut(int **a, int **b, int n, int m, int **c)
{
    b[1][1] = a[1][1];
    for(int i=2;i<=m;i++) b[1][i] = a[1][i] + b[1][i-1];
    for(int i=2;i<=n;i++) b[i][1] = a[i][1] + b[i-1][1];
    for(int i=2;i<=n;i++)
        for(int j=2;j<=m;j++){
            if(b[i-1][j]>b[i][j-1]) b[i][j]=a[i][j]+b[i][j-1];
            else b[i][j]=a[i][j]+b[i-1][j];
        }
    for(int i = 1; i <= n ; i++)
        for(int j = 1; j <= m ; j++)
            c[i][j] = b[n][m];
}
void jeftinPut1(int **a, int **b, int n, int m, int **c)
{
    b[1][1] = a[1][1];
    for(int i=2;i<=m;i++) b[1][i] = a[1][i] + b[1][i-1];
    for(int i=2;i<=n;i++) b[i][1] = a[i][1] + b[i-1][1];
    for(int i=2;i<=n;i++)
        for(int j=2;j<=m;j++){
            if(b[i-1][j]>0 && b[i][j-1]>0){
                if(b[i-1][j]>b[i][j-1]) b[i][j]=a[i][j]+b[i][j-1];
                else b[i][j]=a[i][j]+b[i-1][j];
            }
            else if(b[i-1][j]<0) b[i][j]=a[i][j]+b[i][j-1];
            else if(b[i][j-1]<0) b[i][j]=a[i][j]+b[i-1][j];
        }
}
void blokiraniPut(int **a, int **b, int n, int m, int x, int y, int **c)
{
    int a1=a[x][y];
    a[x][y]=-2147483647;
    jeftinPut1(a, b, n, m, c);
    c[x][y]=b[n][m];
    a[x][y]=a1;
}
void blokirani(int **a, int **b, int n, int m, int **c){
    int p=n,q=m;
    for(int i=n+m;i>3;i--){
        if(p==1) q--;
        else if(q==1) p--;
        else if(b[p-1][q]<b[p][q-1]) p--;
        else q--;
        blokiraniPut(a, b, n, m, p, q, c);
    }
}
int main(){
    int n, m, q;
    cin >> n >> m;

    int** c = new int*[n];
    for (int i = 1; i <= n; ++i)
        c[i] = new int[m]+1;

    int** a = new int*[n];
    for (int i = 1; i <= n; ++i)
        a[i] = new int[m]+1;

    for(int i = 1; i <= n ; i++)
        for(int j = 1; j <= m ; j++)
            cin >> a[i][j];

    int** b = new int*[n];
    for (int i = 1; i <=  n; ++i)
        b[i] = new int[m]+1;

    for(int i = 1; i <= n ; i++)
        for(int j = 1; j <= m ; j++)
            b[i][j] = 0;
    cin>>q;
    int x[q], y[q];
    for(int i=0;i<q;i++)
        cin>>x[i]>>y[i];
    jeftinPut(a,b,n,m,c);
    blokirani(a,b,n,m,c);
    for(int i=0;i<q;i++)
        cout<<c[x[i]][y[i]]<<endl;

    for (int i = 0; i < n; ++i)
        delete [] a[i];
    delete [] a;
    for (int i = 0; i < n; ++i)
        delete [] b[i];
    delete [] b;
    for (int i = 0; i < n; ++i)
        delete [] c[i];
    delete [] c;
}