#include<bits/stdc++.h>
using namespace std;
//とおしたいね
typedef long long int64;
//同じ商品で何回かタイムせーるしてる？
//うーん//
struct Sale{
  int g;
  int64 d;
  int s, e;
} sale[8];
struct State{
  unsigned char x, y, time;
  unsigned bit, used_pop; //used_pop: 使った商品
  State( unsigned char x, unsigned char y, unsigned bit, unsigned char time, unsigned used_pop):x(x),y(y),bit(bit),time(time),used_pop(used_pop){};
};

int W, H, N, sy, sx;
bool used[20][20][1 << 8][101];
char m[20][20];
int64 cost[1 << 8];
const int dy[] = { 0, 1, 0, -1, 0}, dx[] = { 1, 0, -1, 0, 0};
queue< State > que;
int64 BFS(){
  que.push(State( sx, sy, 0, 0, 0));
  used[sy][sx][0][0] = true;
  memset( used, false, sizeof(used));
  for(int i = 0; i < 1 << N; i++){
    cost[i] = 0;
    for(int j = 0; j < N; j++){
      if((i >> j) & 1){
        cost[i] += sale[j].d;
      }
    }
  }
  int64 ret = 0;
  while(!que.empty()){
    State p = que.front(); que.pop();
    ret = max( ret, cost[p.bit]);
    if(p.time > 100) continue;
    for(int i = 0; i < 4; i++){
      int ny = p.y + dy[i], nx = p.x + dx[i];
      if(ny < 0 || ny >= H || nx < 0 || nx >= W) continue;
      if(isdigit(m[ny][nx])){
        for(int j = 0; j < N; j++){
          if(m[ny][nx] - '0' == sale[j].g && !((p.bit >> j) & 1) && !((p.used_pop >> sale[j].g) & 1)){
            if(sale[j].s <= p.time && p.time < sale[j].e){
              if(used[p.y][p.x][p.bit|(1 << j)][p.time]++) continue;
              que.push( State( p.x, p.y, p.bit|(1 << j), p.time,
                               p.used_pop|(1 << sale[j].g)));
            }
          }
        }
      }
    }
    for(int i = 0; i < 5; i++){
      int ny = p.y + dy[i], nx = p.x + dx[i];
      if(ny < 0 || ny >= H || nx < 0 || nx >= W || m[ny][nx] != '.') continue;
      if(used[ny][nx][p.bit][p.time + 1]++) continue;
      que.push( State( nx, ny, p.bit, p.time + 1, p.used_pop));
    }
  }
  return ret;
}

int main(){
  while(cin >> W >> H, W||H){
    for(int i = 0; i < H; i++){
      for(int j = 0; j < W; j++){
        cin >> m[i][j];
        if(m[i][j] == 'P') m[i][j] = '.', sy = i, sx = j;
      }
    }
    cin >> N;
    for(int i = 0; i < N; i++){
      cin >> sale[i].g >> sale[i].d >> sale[i].s >> sale[i].e;
    }
    cout << BFS() << endl;
  }
  return 0;
}