using System;
using System.Collections.Generic;
using System.Linq;

namespace _3
{
    internal class Program
    {
        public struct Tacka
        {
            public int x;
            public int y;
            public Tacka(int x, int y)
            {
                this.x = x;
                this.y = y;
            }

        }
        static void Main(string[] args)
        {

            int x, y;
            int brojPrepreka = 0;

            string[] tmp = Console.ReadLine().Split(' ');
            x = Convert.ToInt32(tmp[0]);
            y = Convert.ToInt32(tmp[1]);
            int[,] mapa = new int[x, y];

            for (int i = 0; i < x; i++)
            {
                int b = 0;
                tmp = Console.ReadLine().Split(' ');
                foreach (string s in tmp)
                {
                    mapa[i, b++] = Convert.ToInt32(s);
                }
            }

            brojPrepreka = Convert.ToInt32(Console.ReadLine());

            Tacka[] prepreke = new Tacka[brojPrepreka];
            for (int i = 0; i < brojPrepreka; i++)
            {
                string[] str = Console.ReadLine().Split(' ');
                prepreke[i] = new Tacka(Convert.ToInt32(str[0]), Convert.ToInt32(str[1]));
            }

            for (int p = 0;p<brojPrepreka;p++)
            {

                List<Tacka> NeposeceneTacke = new List<Tacka>();

                for (int i = 0; i < x; i++)
                {
                    int b = 0;
                    foreach (string s in tmp)
                    {
                        NeposeceneTacke.Add(new Tacka(i, b++));
                    }
                }

                int UkupanPut = 0;

               // Dictionary<Tacka, Tacka> put = new Dictionary<Tacka, Tacka>();//konacan put
                Dictionary<Tacka, Int32> poznateVrednosti = new Dictionary<Tacka, Int32>();//najkraca udaljenost od pocetka

                for (int i = 0; i < x; i++)
                {
                    for (int j = 0; j < y; j++)
                    {
                        poznateVrednosti[new Tacka(i, j)] = Int32.MaxValue;
                        //Console.Write(i + "," + j + " || ");
                    }
                    //Console.Write("\n");
                }
                poznateVrednosti[new Tacka(0, 0)] = mapa[0, 0];



                while (NeposeceneTacke.Count > 0)
                {
                    Tacka tacka = poznateVrednosti.Where(m => NeposeceneTacke.Contains(m.Key)).OrderBy(m => m.Value).First().Key;
                    //Console.WriteLine(tacka.x + ":" + tacka.y + "=" + poznateVrednosti[tacka] + "  ||  ");
                    if (tacka.x == x - 1 && tacka.y == y - 1)
                    {
                        UkupanPut = poznateVrednosti[new Tacka(x - 1, y - 1)];
                    }

                    foreach (Tacka komsa in UzmiKomsije(tacka, x, y, NeposeceneTacke))
                    {
                        if ((mapa[komsa.x, komsa.y] + poznateVrednosti[tacka] < poznateVrednosti[komsa]))
                        {

                            //Console.WriteLine(tacka.x + ":" + tacka.y + "=" + poznateVrednosti[tacka] + "  ||  ");
                            if (!((prepreke[p].x-1 == komsa.x && prepreke[p].y-1 == komsa.y) || (prepreke[p].x-1 == tacka.x && prepreke[p].y-1 == tacka.y)))
                            {
                                poznateVrednosti[komsa] = mapa[komsa.x, komsa.y] + poznateVrednosti[tacka];
                                //put[tacka] = komsa;
                            }
                        }

                    }
                    NeposeceneTacke.Remove(tacka);

                }

                Console.WriteLine(UkupanPut);
                //Console.ReadLine();
                //depth first search


            }
        }

        public static List<Tacka> UzmiKomsije(Tacka takca, int duzinax, int duzinay, List<Tacka> NeposeceneTacke)
        {
            List<Tacka> komsije = new List<Tacka>();

            if (takca.x > 0 && NeposeceneTacke.Contains(new Tacka(takca.x - 1, takca.y)))
            {
                komsije.Add(new Tacka(takca.x - 1, takca.y));
            }
            
            if (takca.x < duzinax - 1 && NeposeceneTacke.Contains(new Tacka(takca.x + 1, takca.y)))
            {
                komsije.Add(new Tacka(takca.x + 1, takca.y));
            }
            if (takca.y > 0 && NeposeceneTacke.Contains(new Tacka(takca.x, takca.y - 1)))
            {
                komsije.Add(new Tacka(takca.x, takca.y - 1));
            }
            if (takca.y < duzinay - 1 && NeposeceneTacke.Contains(new Tacka(takca.x, takca.y + 1)))
            {
                komsije.Add(new Tacka(takca.x, takca.y + 1));
            }

            return komsije;
        }
        
    }
}