### constant

DIRCS = [[1, 0], [0, -1], [-1, 0], [0, 1]]

### subroutines

def neighbors(pos)
  x = pos % $w
  y = pos / $w
  for dxy in DIRCS
    x0 = x + dxy[0]
    y0 = y + dxy[1]
    if x0 >= 0 && x0 < $w && y0 >= 0 && y0 < $h
      yield (y0 * $w + x0)
    end
  end
end

def max_discount(t, pos, gbits)
  return 0 if t >= $max_et
  return $mdcache[t][pos][gbits] if ! $mdcache[t][pos][gbits].nil?

  dsum = 0
  gbits0 = gbits

  for g in $brds[pos]
    if t >= $goods[g][1] && t < $goods[g][2]
      gb = (1 << g)
      if (gbits0 & gb) != 0
        dsum += $goods[g][0]
        gbits0 ^= gb
      end
    end
  end

  max_d0 = 0

  for pos0 in $npos[pos]
    d0 = max_discount(t + 1, pos0, gbits0)
    if max_d0 < d0
      max_d0 = d0
    end
  end

  $mdcache[t][pos][gbits] = dsum + max_d0
end

### main

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

  start = nil
  $brds = $wh.times.map{[]}

  for y in (0...$h)
    hl = gets.strip.split(' ')
    for x in (0...$w)
      pos = y * $w + x
      case hl[x]
      when 'P'
        start = pos
      when '0'..'9'
        g = hl[x].to_i
        neighbors(pos) do |pos0|
          if ! $brds[pos0].nil? && ! $brds[pos0].include?(g)
            $brds[pos0] << g
          end
        end
        $brds[pos] = nil
      end
    end
  end
  #p pos
  #p $brds

  $npos = []
  for pos in (0...$wh)
    if ! $brds[pos].nil?
      nbrs = []
      neighbors(pos) do |pos0|
        if ! $brds[pos0].nil?
          nbrs << pos0
        end
      end
      $npos[pos] = nbrs
    end
  end
  #p $npos

  $n = gets.strip.to_i
  $goods = []
  $max_et = 0

  $n.times do
    g, d, st, et = gets.strip.split(' ').map{|s| s.to_i}
    $goods[g] = [d, st, et]
    $max_et = et if $max_et < et
  end
  #p $goods
  #p $max_et

  $mdcache = $max_et.times.map{$wh.times.map{[]}}

  max_d = max_discount(0, start, (1 << $n) - 1)
  puts max_d
end