#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include "bits/stdc++.h"
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}

#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);

using namespace std;
//using namespace __gnu_pbds;
/*
ll add(ll x,ll y){
    x+=y;
    if(x<0){
        x%=mod;
        x+=mod;
    }else{
        if(x>=mod) x%=mod;
    }
    return x;
}
ll mul(ll a,ll b){
	ll ans = (a*b)%mod;
	if(ans<0) ans+=mod;
	return ans;
}
typedef tree<int,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<int,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
    return uniform_int_distribution<ll>(l,r)(rng);
}
*/
ll n,m,q;
vector<vector<ll> > a;
vector<vector<ll> > dpl,dpr,dp,ps,suf;
void tc(){
    cin >> n >> m;
    a.resize(n+2);
    dpl.resize(n+2);
    dpr.resize(n+2);
    dp.resize(n+2);
    ps.resize(n+2);
    suf.resize(n+2);
    for(ll i = 0;i<=n+1;i++) a[i].resize(m+2);
    for(ll i = 0;i<=n+1;i++) dpl[i].resize(m+2);
    for(ll i = 0;i<=n+1;i++) dpr[i].resize(m+2);
    for(ll i = 0;i<=n+1;i++) dp[i].resize(m+2);
    for(ll i = 0;i<=n+1;i++) ps[i].resize(m+2);
    for(ll i = 0;i<=n+1;i++) suf[i].resize(m+2);
    for(ll i = 1;i<=n;i++){
        for(ll j = 1;j<=m;j++) cin >> a[i][j];
    }
    for(ll i = 0;i<=n+1;i++) for(ll j = 0;j<=m+1;j++){
        dpl[i][j] = dpr[i][j] = dp[i][j] = ps[i][j] = suf[i][j] = llinf;
    }
    dpl[0][1] = 0;
    dpr[n+1][m] = 0;
    for(ll i = 1;i<=n;i++){
        for(ll j = 1;j<=m;j++){
            dpl[i][j] = a[i][j] + min(dpl[i-1][j],dpl[i][j-1]);
        }
    }
    for(ll i = n;i>=1;i--){
        for(ll j = m;j>=1;j--){
            dpr[i][j] = a[i][j] + min(dpr[i+1][j],dpr[i][j+1]);
        }
    }
    vector<pll> vl = {{-1,0},{0,-1}},vr = {{1,0},{0,1}};
    for(ll i = 1;i<=n;i++) for(ll j = 1;j<=m;j++){
        for(pll pl : vl){
            for(pll pr : vr){
                ll li = i+pl.fi,lj = j+pl.sc;
                ll ri = i+pr.fi,rj = j+pr.sc;
                dp[i][j] = min(dp[i][j],a[i][j]+dpl[li][lj]+dpr[ri][rj]);
            }
        }
    }
    for(ll i = 1;i<=n;i++){
        for(ll j = 1;j<=m;j++){
            ps[i][j] = min(ps[i][j-1],dp[i][j]);
        }
    }
    for(ll i = 1;i<=n;i++){
        for(ll j = m;j>=1;j--){
            suf[i][j] = min(suf[i][j+1],dp[i][j]);
        }
    }
    cin >> q;
    while(q--){
        ll x,y;
        cin >> x >> y;
        ll ans = min(ps[x+1][y-1],suf[x-1][y+1]);
        if(ans>=llinf) ans = -1;
        cout<<ans<<endl;
    }
}
int main(){
	daj_mi_malo_vremena
    int t; t = 1;
    while(t--){
        tc();
    }
	return 0;
}