#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const ll MAXN = 1000000000000000;
int a[1000001];

ll b[1000001];

int n,m;

void pripremi(){

    for (int i = 1; i <= n; i++){
         for (int j = 1; j <= m; j++){
             if(i == 1){
                if(j == 1)
                    b[m*(i-1)+j] = a[m*(i-1)+j];
                else
                    b[m*(i-1)+j] = b[m*(i-1)+j-1] + a[m*(i-1)+j];
             }
             else if (j == 1) b[m*(i-1)+j] = b[m*(i-2)+j] + a[m*(i-1)+j];
             else{
                b[m*(i-1)+j] = min(b[m*(i-1)+j-1], b[m*(i-2)+j]) + a[m*(i-1)+j];
             }

         }
     }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

     int q,x,y,ind,c;
     ll val;
     cin >> n >> m;

    c = n * m;
     for (int i = 1; i <= n; i++){
         for (int j = 1; j <= m; j++){
             cin >> a[m*(i-1)+j];
         }
     }
     cin >> q;
     while(q--){
        bool ind = false;
       cin >> x >> y;
        pripremi();
       ind = m*(x-1)+y;

        b[ind] = MAXN;


        for (int i = ind+1; i <= ind+(m-y); i++){
            val = b[i];
            if(x == 1) {b[i] = MAXN;}
            else{
                b[i] = min(b[i-1], b[i - m]) + a[i];
            }
            if(b[i] == val)
                break;
        }
         for (int i = ind+m; i <= c; i+=m){
            val = b[i];
            if(y == 1) {b[i] = MAXN;}
            else{
                b[i] = min(b[i-1], b[i - m]) + a[i];
            }
            if(b[i] == val)
                break;
        }
        for (int i = x+1; i <= n; i++){
            for (int j = y+1; j <= m; j++){
                val = b[ind];
                ind = m*(i-1)+j;
                b[ind] = min(b[ind-1], b[ind - m]) + a[ind];
                if(val != b[ind])
                {
                    ind = true;
                    break; 
                }
                
            }
        }



        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= m; j++){
                cout << b[m*(i-1)+j] << " ";
            }
            cout << endl;
        }
    }
    return 0;
}