#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void minbroj(vector<vector<int>>&a, vector<vector<int>>&mi, int i, int j, int &n, int &m)
{
	if (i == n) return;
	if (j == m || a[i][j] == -1) {
		minbroj(a, mi, i + 1, 0, n, m);
		return;
	}
	if (i > 0 && j > 0) mi[i][j] = min({ mi[i - 1][j], mi[i][j - 1], mi[i][j] - a[i][j] });
	if (i > 0 && j == 0) mi[i][j] = min(mi[i][j] - a[i][j], mi[i - 1][j]);
	if (i == 0 &&  j > 0) mi[i][j] = min(mi[i][j] - a[i][j], mi[i][j - 1]);
	if (i == 0 && j == 0) mi[i][j] = 0;
	mi[i][j] += a[i][j];
	minbroj(a, mi, i, j + 1, n, m);
}
int main()
{
	int n, m; cin >> n >> m;
	vector<vector<int>>a(n, vector<int>(m));
	for (int i = 0; i < n; i++)
	{
		a[i] = vector<int>(m);
		for (int j = 0; j < m; j++)
		{
			cin >> a[i][j];
		}
	}
	int q; cin >> q;
	for (int qq = 0; qq < q; qq++)
	{
		int x, y; cin >> x >> y;
		x--; y--;
		int privremeno = a[x][y];
		a[x][y] = -1;
		vector<vector<int>>mi(n, vector<int>(m, INT_MAX));
		minbroj(a, mi, 0, 0, n, m);
		a[x][y] = privremeno;
		cout << mi[n - 1][m - 1] << endl;
	}
}