#include "bits/stdc++.h"
#include<unordered_map>
#pragma warning(disable:4996)
using namespace std;
vector<long long int> divisor(long long int n) {
	vector<long long int> res;
	for (long long int i = 1; i * i <= n; ++i) {
		if (n % i == 0) {
			res.push_back(i);
			if (i * i != n) res.push_back(n / i);
		}
	}
	sort(begin(res), end(res));
	return res;
}
int X, Y, n;
vector<vector<int>>getans(const vector<vector<int>>&field, const int now, const vector<int>&nums,const vector<pair<int,int>>&ps) {
	if (now == ps.size())return field;
	vector<long long int>divs(divisor(nums[now]));
	vector<vector<int>>ans;
	const int fromy = ps[now].first;
	const int fromx = ps[now].second;
	for (const  int  yrange : divs) {
		const int xrange = nums[now] / yrange;
		int left = max(0, fromx - xrange + 1);
		int right = min(X-xrange, fromx);
		int up = max(0, fromy - yrange + 1);
		int down = min(Y- yrange, fromy);
		for (int leftside = left; leftside <= right; ++leftside) {
			for (int upside = up; upside <= down; ++upside) {
				bool ok = true;
				for (int x = leftside; x < leftside+xrange; ++x) {
					for (int y = upside; y < upside+yrange; ++y) {
						if (field[y][x] && field[y][x] != now) {
							ok = false;
							break;
						}
					}
				}
				if (ok) {
					vector<vector<int>>nfield(field);
					for (int x = leftside; x < leftside + xrange; ++x) {
						for (int y = upside; y < upside + yrange; ++y) {
							nfield[y][x] = now;
						}
					}
					vector<vector<int>>nans(getans(nfield, now + 1, nums, ps));
					if (!ans.empty()) {
						if (!nans.empty())return vector<vector<int>>();
					}
					else {
						ans = nans;
					}
				}
			}
		}
		
	}
	return ans;
}
int main() {
	while (1) { cin >> X >> Y >> n;
	if (!X)break;
		vector<int>nums(n+1);
		for (int i = 0; i < n; ++i) {
			int x, y; cin >> x >> y;
			nums[x] = y;
		}
		vector<vector<int>>field(Y, vector<int>(X));
		vector<pair<int, int>>froms(n + 1);
		for (int y = 0; y < Y; ++y) {
			for (int x = 0; x < X; ++x) {
				int a; cin >> a;
				field[y][x] = a;
				if (a) {
					froms[a] = make_pair(y, x);
				}
			}
		}
		auto v=getans(field, 1, nums, froms);
		if (v.empty()) {
			cout << "NA" << endl;
		}
		else {
			for (int i = 0; i < Y; ++i) {
				for (int j = 0; j < X; ++j) {
					cout << v[i][j];
					if (j != X - 1)cout << " ";
				}
				cout << endl;
			}
		}
	}
	return 0;
}