using System;

namespace ConsoleApp31
{
    class Program
    {
        static void Main(string[] args)
        {
            int n, m;
            string s = Console.ReadLine();
            n = Int32.Parse(s.Split(" ")[0]);
            m = Int32.Parse(s.Split(" ")[1]);
            int[,] a3 = new int[n,m];
            for(int i=0;i<n;i++)
            {
                string b = Console.ReadLine();
                for (int j = 0; j < m; j++)
                {
                    a3[i, j] = Int32.Parse(b.Split(" ")[j]);
                }
            }
            int q;
            q= Int32.Parse(Console.ReadLine());
            int[] a1 = new int[q];
            for (int k = 0; k < q; k++)
            {
                int[,] a = (int[,])a3.Clone();
                int x, y;
                string s1 = Console.ReadLine();
                x = Int32.Parse(s1.Split(" ")[0]) - 1;
                y = Int32.Parse(s1.Split(" ")[1]) - 1;
                a[x, y] = 0;
                for (int i = n-2; i >= 0; i--)
                {
                    if (a[i, m - 1] != 0)
                        a[i, m - 1] = a[i + 1, m - 1] + a[i, m - 1];
                    if (a[i + 1, m - 1] == 0)
                        a[i, m - 1] = 0;
                }
                for (int i = m - 2; i >= 0; i--)
                {
                    if (a[n - 1, i] != 0)
                        a[n - 1, i] = a[n - 1, i + 1] + a[n - 1, i];
                    if (a[n - 1, i + 1] == 0)
                        a[n - 1, i + 1] = 0;
                }
                for (int i = n-2; i >= 0; i--)
                {
                    for (int j = m-2; j >= 0; j--)
                    {
                        if (a[i,j]!=0)
                        {
                            if (a[i+1, j] == 0)
                            {
                                a[i, j] = a[i, j] + a[i, j + 1];
                            }
                            else if (a[i, j+1] == 0)
                            {
                                a[i, j] = a[i, j] + a[i + 1, j];
                            }
                            else
                            {
                                a[i, j] = a[i, j] + Math.Min(a[i + 1, j], a[i, j + 1]);
                            }
                        }
                    }
                }
                a1[k] = a[0, 0];
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        Console.Write(a[i, j] + " ");
                    }
                    Console.WriteLine();
                }
            }
            foreach(var x in a1)
            {
                Console.WriteLine(x);
            }
        }
    }
}