### subroutines

def divk(k)
  for i in (1..k)
    if k % i == 0
      yield k / i, i
    end
  end
end

def fit?(b, rect, grnds)
  x1, y1, x2, y2 = rect

  for y in (y1..y2)
    for x in (x1..x2)
      return false if grnds[y][x] > 0 && grnds[y][x] != b
    end
  end
  true
end

def gen_rects(b, k, x, y, grnds, w, h)
  rects = []

  divk(k) do |w0, h0|
    next if w0 > w || h0 > h

    for y0 in (0...h0)
      y1 = y - y0
      y2 = y + h0 - 1 - y0
      next if y1 < 0 || y2 >= h

      for x0 in (0...w0)
        x1 = x - x0
        x2 = x + w0 - 1 - x0
        next if x1 < 0 || x2 >= w

        rect = [x1, y1, x2, y2]

        if fit?(b, rect, grnds)
          rects << rect
        end
      end
    end
  end

  rects
end

def fill_common(b, rects, grnds, w, h)
  counts = grnds.map{|hl| hl.map{0}}

  for r in rects
    x1, y1, x2, y2 = r
    for y in (y1..y2)
      for x in (x1..x2)
        counts[y][x] += 1
      end
    end
  end

  changed = false
  rnum = rects.length

  for y in (0...h)
    for x in (0...w)
      if counts[y][x] == rnum && grnds[y][x] == 0
        grnds[y][x] = b
        changed = true
      end
    end
  end

  changed
end

### main

while true
  w, h, n = gets.strip.split(' ').map{|s| s.to_i}
  break if w + h + n == 0

  buyers = []
  ksum = 0

  n.times.each do
    b, k = gets.strip.split(' ').map{|s| s.to_i}
    buyers[b - 1] = [b, k]
    ksum += k
  end

#  if ksum != w * h
#    h.times.each{gets}
#    puts 'NA'
#    next
#  end

  grnds = []

  for y in (0...h)
    hl = gets.strip.split(' ').map{|s| s.to_i}
    grnds << hl
  end

  for y in (0...h)
    for x in (0...w)
      b = grnds[y][x]
      if b > 0
        b0 = buyers[b - 1]
        b0[2] = gen_rects(b0[0], b0[1], x, y, grnds, w, h)
      end
    end
  end
  #p grnds
  #p buyers

  while ! buyers.empty?
    changed = false

    for b0 in buyers
      for r in b0[2].clone
        if ! fit?(b0[0], r, grnds)
          b0[2].delete(r)
          changed = true
        end
      end
    end

    for b0 in buyers.clone
      changed = fill_common(b0[0], b0[2], grnds, w, h) || changed
      rnum = b0[2].length
      if rnum == 1
        buyers.delete(b0)
        changed = true
      elsif rnum == 0
        changed = false
        break
      end
    end
    #p buyers

    break if ! changed
  end

  if buyers.length > 0
    puts 'NA'
  else
    for hl in grnds
      puts hl.join(' ')
    end
  end
end