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

int main()
{
    long long n, m, a, q, x, y;
    cin >> n >> m;
    vector<vector<long long>> v(n), valt(n), vsum(n); 

    for (int i = 0; i < m; i++)
    {
        cin >> a;
        v[0].push_back(a);
        if (i == 0)
            vsum[0].push_back(a);
        else
            vsum[0].push_back(vsum[0][i-1] + a);
    }

    for (int i = 1; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> a;
            v[i].push_back(a);
            if (j == 0)
                vsum[i].push_back(vsum[i-1][0] + a);
            else
                vsum[i].push_back(min(vsum[i-1][j] + a, vsum[i][j-1] + a));
        }
    }

    cin >> q;
    for (int i = 0; i < q; i++)
    {
        valt = vsum;
        cin >> x >> y; x--; y--;
        valt[x][y] = numeric_limits<long long int>::max();
        for (int j = x; j < x; j++)
        {
            for (int j = 0; j < m; j++)
            {
                cin >> a;
                valt[i][j](a);
                if (j == 0)
                vsum[i].push_back(vsum[i-1][0] + a);
                else
                    v[i].push_back(min(vsum[i-1][j] + a, vsum[i][j-1] + a));
            }
        }

    }

    for (auto x : valt)
    {    
        for (auto y : x)
            cout << y << " ";
        cout << endl;
    }


    return 0;
}