#include <iostream>
#include <vector>
#include <string>
#include <utility>
using namespace std;

#define fi first
#define se second
typedef pair<int,int> pii;

int X, Y, n;
vector<int> v;
vector<vector<int> > board;
vector<vector<int> > ans;
vector<vector<int> > out;
int ok[10][10][10][10];
bool use[16];

int solve(int y, int x) {
	int res = 0;
	if(y >= Y) {
		out = ans;
		return 1;
	}
	if(ans[y][x]) {
		return solve(y+(x+1)/X, (x+1)%X);
	}
	for(int i = y; i < Y; i++) {
		for(int j = x; j < X; j++) {
			if(ok[y][x][i][j] && !use[ok[y][x][i][j]]) {
				use[ok[y][x][i][j]] = true;
				for(int k = y; k <= i; k++)
					for(int l = x; l <= j; l++)
						ans[k][l] = ok[y][x][i][j];
				res += solve(y+(x+1)/X, (x+1)%X);
				for(int k = y; k <= i; k++)
					for(int l = x; l <= j; l++)
						ans[k][l] = 0;
				use[ok[y][x][i][j]] = false;
			}
		}
	}
	return res;
}

int main() {
	while(cin >> X >> Y >> n, X || Y || n) {
		v.clear(); board.clear(); ans.clear();
		out.clear();
		v.resize(n+1);
		board.resize(Y);
		ans.resize(Y);
		for(int i = 0; i < 10*10*10*10; i++)
			ok[0][0][0][i] = 0;
		for(int i = 0; i < 16; i++)
			use[i] = false;
		for(int i = 1; i < v.size(); i++) v[i] = -1;
		for(int i = 0; i < n; i++) {
			int b, k;
			cin >> b >> k;
			v[b] = k;
		}
		for(int i = 1; i < v.size(); i++) {
			if(v[i] < 0) {
				cout << "NA" << endl;
				continue;
			}
		}
		for(int i = 0; i < Y; i++) {
			board[i].resize(X);
			ans[i].resize(X);
			for(int j = 0; j < X; j++) {
				cin >> board[i][j];
				ans[i][j] = 0;
			}
		}

		for(int i = 0; i < Y; i++) {
			for(int j = 0; j < X; j++) {
				for(int k = i; k < Y; k++) {
					for(int l = j; l < X; l++) {
						int cnt = 0;
						for(int m = 1; m < v.size(); m++) {
							if((k-i+1) * (l-j+1) == v[m]) {
								bool flg = true;
								for(int y = 0; y < Y && flg; y++) {
									for(int x = 0; x < X && flg; x++) {
										if(i <= y && y <= k && j <= x && x <= l) {
											if(board[y][x] != 0 && board[y][x] != m)
												flg = false;
										}
										else {
											if(board[y][x] == m)
												flg = false;
										}
									}
								}
								if(flg) {
									ok[i][j][k][l] = m;
									cnt++;
								}
							}
						}
						if(cnt != 1) ok[i][j][k][l] = 0;
					}
				}
			}
		}

		int res = 0;
		res = solve(0,0);
		int sum = 0;
		for(int i = 0; i < v.size(); i++) sum += v[i];
		bool flg = true;
		for(int i = 0; i < out.size(); i++)
			for(int j = 0; j < out[i].size(); j++)
				flg &= out[i][j] != 0;
		if(res == 1 && sum == X*Y && flg) {
			for(int i = 0; i < out.size(); i++) {
				cout << out[i][0];
				for(int j = 1; j < out[i].size(); j++) {
					cout << " " << out[i][j];
				}
				cout << endl;
			}
		}
		else {
			cout << "NA" << endl;
		}
	}
}