#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main()
{
    int n,a,b,m,q,x,y;
    int zbir=0;
    cin >> n>> m;
    bool posecen[100][100];
    vector<vector<int>>v(n);
    for (int i = 1;i <= n;i++)
        for (int j=1;j<=m;j++) {
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    for (int i = 1;i <= n;i++)
        for (int j=1;j<=m;j++)
            posecen[i][j] = false;
    cin>>q;
    bool blokiran;
    queue<int>q1;
    q1.push(k);
    posecen[1][1] = true;

    while (!q1.empty())
    {
        int s = q1.front();
        q1.pop();
        for (int i = 0;i < v[s].size();i++)
        {
            if (posecen[v[s][i]] == false)
            {
                posecen[v[s][i]] = true;
                q.push(v[s][i]);

            }
        }
        for (int i=1;i<=q;i++)
        {
        cin>>x>>y;
        posecen[x][y]=false;
        }
    }
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)
            {
                if (posecen[i][j]==true)
                zbir=zbir+a[i][j];
            }
}