#include <bits/stdc++.h>
using namespace std;
int N, M, K;
int P[110][110], C[110][2], T[110][110];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int D[110][110][(1 << 7) + 10];
bool in[110][110], cov[110][110];
pair<int, pair<int, int> > pre[110][110][(1 << 7) + 10];
queue<pair<int, int> > Q;
void spfa(int msk) {
  while (!Q.empty()) {
    int x, y, xx, yy;
    x = Q.front().first;
    y = Q.front().second;
    Q.pop();
    in[x][y] = false;
    for (int i = 0; i < 4; i++) {
      xx = x + dx[i], yy = y + dy[i];
      if (xx >= 1 && xx <= N && yy >= 1 && yy <= M &&
          (D[xx][yy][msk] == -1 || D[xx][yy][msk] > D[x][y][msk] + P[xx][yy])) {
        D[xx][yy][msk] = D[x][y][msk] + P[xx][yy];
        pre[xx][yy][msk] = make_pair(msk, make_pair(x, y));
        if (!in[xx][yy]) {
          Q.push(make_pair(xx, yy));
          in[xx][yy] = true;
        }
      }
    }
  }
}
void dfs(int x, int y, int msk) {
  if (msk == 0) return;
  cov[x][y] = true;
  pair<int, pair<int, int> > pi = pre[x][y][msk];
  if (pi.first == -1) return;
  dfs(pi.second.first, pi.second.second, pi.first);
  dfs(pi.second.first, pi.second.second, msk ^ pi.first);
}
int main() {
  scanf("%d%d%d", &N, &M, &K);
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= M; j++) scanf("%d", &P[i][j]);
  for (int i = 0; i < K; i++) scanf("%d%d", &C[i][0], &C[i][1]);
  memset(D, -1, sizeof(D));
  memset(T, -1, sizeof(T));
  memset(pre, -1, sizeof(pre));
  memset(cov, false, sizeof(cov));
  memset(in, false, sizeof(in));
  for (int x = 1; x <= N; x++)
    for (int y = 1; y <= M; y++) D[x][y][0] = 0;
  for (int i = 0; i < K; i++) {
    int x = C[i][0], y = C[i][1];
    D[x][y][(1 << i)] = P[x][y];
    T[x][y] = i;
  }
  for (int i = 1; i < (1 << K); i++) {
    for (int x = 1; x <= N; x++)
      for (int y = 1; y <= M; y++) {
        if (T[x][y] != -1 && (i & (1 << T[x][y])) == 0) continue;
        for (int j = (i - 1) & i; j; j = (j - 1) & i)
          if (D[x][y][j] != -1 && D[x][y][i ^ j] != -1 &&
              (D[x][y][i] > D[x][y][j] + D[x][y][i ^ j] - P[x][y] ||
               D[x][y][i] == -1)) {
            D[x][y][i] = D[x][y][j] + D[x][y][i ^ j] - P[x][y];
            pre[x][y][i] = make_pair(j, make_pair(x, y));
          }
        if (D[x][y][i] != -1) {
          in[x][y] = true;
          Q.push(make_pair(x, y));
        }
      }
    spfa(i);
  }
  int ans = -1, xx, yy;
  for (int x = 1; x <= N; x++)
    for (int y = 1; y <= M; y++)
      if (D[x][y][(1 << K) - 1] != -1 &&
          (ans == -1 || D[x][y][(1 << K) - 1] < ans)) {
        ans = D[x][y][(1 << K) - 1];
        xx = x, yy = y;
      }
  printf("%d\n", ans);
  dfs(xx, yy, (1 << K) - 1);
  for (int x = 1; x <= N; x++) {
    for (int y = 1; y <= M; y++)
      if (cov[x][y])
        printf("%c", 'X');
      else
        printf("%c", '.');
    printf("\n");
  }
  return 0;
}
