#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > L;
int N, M, K;
int T[1 << 7][205];
int tr[1 << 7][205];
int D[105][105];
char ans[105][105];
int xx[4] = {-1, 0, 1, 0};
int yy[4] = {0, 1, 0, -1};
int hsh(int x, int y) { return x * M + y; }
void trace(int ad, int x, int y) {
  ans[x][y] = 'X';
  if (T[ad][hsh(x, y)] == D[x][y]) return;
  int d = tr[ad][hsh(x, y)];
  if (d <= 0)
    trace(ad, x - xx[-d], y - yy[-d]);
  else
    trace(d, x, y), trace(ad ^ d, x, y);
}
int main() {
  scanf("%d%d%d", &N, &M, &K);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      scanf("%d", D[i] + j);
      ans[i][j] = '.';
    }
  }
  for (int i = 0; i < K; i++) {
    int A, B;
    scanf("%d%d", &A, &B);
    A--, B--;
    L.push_back(pair<int, int>(A, B));
  }
  for (int i = 1; i < 1 << K; i++) {
    for (int j = 0; j < N * M; j++) T[i][j] = 0x3fffffff;
  }
  for (int i = 0; i < K; i++) {
    T[1 << i][hsh(L[i].first, L[i].second)] = D[L[i].first][L[i].second];
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      T[0][hsh(i, j)] = D[i][j];
    }
  }
  for (int i = 1; i < 1 << K; i++) {
    for (int j = i; j; j = (j - 1) & i) {
      for (int k = 0; k < N; k++) {
        for (int l = 0; l < M; l++) {
          int tmp = T[i][hsh(k, l)];
          T[i][hsh(k, l)] = min(
              T[i][hsh(k, l)], T[j][hsh(k, l)] + T[i ^ j][hsh(k, l)] - D[k][l]);
          if (T[i][hsh(k, l)] != tmp) tr[i][hsh(k, l)] = j;
        }
      }
    }
    bool chk[105][105] = {0};
    for (int t = 0; t < N * M; t++) {
      int rx = -1, ry = -1;
      for (int x = 0; x < N; x++) {
        for (int y = 0; y < M; y++) {
          if (!chk[x][y] && (rx == -1 || T[i][hsh(rx, ry)] > T[i][hsh(x, y)]))
            rx = x, ry = y;
        }
      }
      chk[rx][ry] = true;
      for (int d = 0; d < 4; d++) {
        int px = rx + xx[d], py = ry + yy[d];
        if (px < 0 || px >= N || py < 0 || py >= M) continue;
        int tmp = T[i][hsh(px, py)];
        T[i][hsh(px, py)] =
            min(T[i][hsh(px, py)], T[i][hsh(rx, ry)] + D[px][py]);
        if (T[i][hsh(px, py)] != tmp) tr[i][hsh(px, py)] = -d;
      }
    }
  }
  int rx = 0, ry = 0, to = (1 << K) - 1;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (T[to][hsh(i, j)] < T[to][hsh(rx, ry)]) rx = i, ry = j;
    }
  }
  trace(to, rx, ry);
  printf("%d\n", T[to][hsh(rx, ry)]);
  for (int i = 0; i < N; i++) printf("%s\n", ans[i]);
  return 0;
}
