#include <iostream>
#include <string>
#include <vector>


using namespace std;

int n, m;
vector<pair<string, int>> vec;
void dfs(vector<vector<int>> matrix, int i, int j, string p, int w) {
    if (i < n && j < m) {
        if (i == n-1 && j == m-1) {
            w += matrix[i][j];
            vec.push_back(make_pair(p, w));
        }
        else
        {
            w += matrix[i][j];
            dfs(matrix, i + 1, j,p+"(" + to_string(i+2) + "," + to_string(j+1) + ")",w);
            dfs(matrix, i, j + 1,p+"(" + to_string(i+1) + "," + to_string(j+2) + ")",w);
        }
    }
}


int main()
{
    cin >> n >> m;
    vector<vector<int>> matrix(n,vector<int>(m));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            int tempVal;
            cin >> tempVal;
            matrix[i][j] = tempVal;
        }
    }

    dfs(matrix, 0, 0,"(1,1)",0);
    int k;
    cin >> k;
    for (size_t i = 0; i < k; i++)
    {
        string x, y,s;
        int min = INT32_MAX;
        cin >> x >> y;
        s = "("+x+","+y+")";
        for (auto r:vec)
        {
            size_t found = r.first.find(s);
            if (found == string::npos && r.second < min)
            {
                //cout << r.first << r.second << endl;
                min = r.second;
            }
        }
        cout << min<<endl;
    }
}