#include <iostream>
using namespace std;
int n, m, k;
int arr[101][101];
int dR[9] = {-1, -1, -1, 0, 1, 1, 1, 0, 0};
int dC[9] = { -1, 0, 1, 1, 1, 0, -1, -1, 0};
int dR1[4] = {-1, 0, 1, 0};
int dC1[4] = {0, 1, 0, -1};
typedef struct Point {
int x, y;
};
Point C[201];
int main() {
int t;
cin >> t;
for (int tc = 1; tc <= t; tc++) {
cin >> n >> m >> k;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
}
}
for (int i = 0; i < k; i++) {
cin >> C[i].x >> C[i].y;
}
int count = 0;
for (int i = 0; i < k; i++) {
for (int h = 0; h < 9; h++) {
int x = C[i].x + dR[h];
int y = C[i].y + dC[h];
if (x >= 0 && x < n && y >= 0 && y < m) {
arr[x][y] = 0;
}
}
count++;
if (count == 5) {
for (int p = 0; p < n; p++) {
for (int q = 0; q < m; q++) {
if (arr[p][q] != 0 && arr[p][q] != 10) {
arr[p][q] += 1;
}
}
}
for (int p = 0; p < n; p++) {
for (int q = 0; q < m; q++) {
if (arr[p][q] == 10) {
for (int h = 0; h < 4; h++) {
int xx = p + dR1[h];
int yy = q + dC1[h];
if (xx >= 0 && xx < n && yy >= 0 && yy < m && arr[xx][yy] == 0) {
arr[xx][yy] = 1;
}
}
}
}
}
count = 0;
}
}
int cf = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j] != 0)
cf++;
}
}
cout << "#" << tc << " ";
cout << cf << endl;
}
}