#include <iostream>
#include <bits/stdc++.h>

using namespace std;

/*
struct upd
{
    long long int i;
    long long int j;
    long long int val;
};
*/

int main()
{
    long long int n, m, i, j, q, x, y;
    cin >> n >> m;
    long long int matrica[n+1][m+1];
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            cin >> matrica[i][j];
        }
    }

    long long int b[n+1][m+1];
    long long int c[n+1][m+1];
    b[1][1]=matrica[1][1];
    c[1][1]=matrica[1][1];
    for(j=2; j<=m; j++)
    {
        b[1][j] = b[1][j-1] + matrica[1][j];
        c[1][j] = c[1][j-1] + matrica[1][j];
    }
    for(i=2; i<=n; i++)
    {
        b[i][1] = b[i-1][1] + matrica[i][1];
        c[i][1] = c[i-1][1] + matrica[i][1];
    }
    for(i=2; i<=n; i++)
    {
        for(j=2; j<=m; j++)
        {
            b[i][j] = min(b[i-1][j], b[i][j-1]) + matrica[i][j];
            c[i][j] = min(c[i-1][j], c[i][j-1]) + matrica[i][j];
        }
    }
    cout << endl;for(i=1; i<=n; i++){for(j=1; j<=m; j++){cout << b[i][j] << " ";}cout << endl;}cout << endl;
    cout << endl;for(i=1; i<=n; i++){for(j=1; j<=m; j++){cout << c[i][j] << " ";}cout << endl;}cout << endl;

    cin >> q;
    while(q--)
    {
        cin >> x >> y;
        /*
        upd Node;
        Node.i = x;
        Node.j = y;
        Node.val = b[Node.i][Node.j];
        b[Node.i][Node.j] = LONG_MAX;
        */

        b[x][y] = LONG_MAX;
        // Horizontalno
        for(j=y+1; j<=m; j++)
        {
            if(x>1)
            {
                b[x][j] = min(b[x][j-1], b[x-1][j]) + matrica[x][j];
            }
            else
            {
                b[x][j] = b[x][j-1]+ matrica[x][j];
            }
        }
        // Vertikalno
        for(i=x+1; i<=n; i++)
        {
            if(y>1)
            {
                b[i][y] = min(b[i-1][y], b[i][y-1]) + matrica[i][y];
            }
            else
            {
                b[i][y] = b[i-1][y] + matrica[i][y];
            }
        }
        // Ostalo
        for(i=x+1; i<=n; i++)
        {
            for(j=y+1; j<=m; j++)
            {
                b[i][j] = min(b[i][j-1], b[i-1][j]) + matrica[i][j];
            }
        }
        cout << endl;for(i=1; i<=n; i++){for(j=1; j<=m; j++){cout << b[i][j] << " ";}cout << endl;}cout << endl;

        cout << b[n][m] << endl;

        // Korekcija
        for(i=x; i<=n; i++)
        {
            for(j=y; j<=m; j++)
            {
                b[i][j] = c[i][j];
            }
        }
        cout << endl;for(i=1; i<=n; i++){for(j=1; j<=m; j++){cout << c[i][j] << " ";}cout << endl;}cout << endl;
    }
}