#include <bits/stdc++.h>
const int maxn = 203, maxmask = 131;
int n, m, r, c, t, root, u[10], d[maxn][maxn], df[maxn][maxn], used[maxn],
    dp[maxn][maxmask], pf[maxn][maxmask], bf[maxn][maxmask];
void markAns(int x, int y) {
  if (df[x][y] < 0) {
    used[x] = used[y] = 1;
  } else {
    markAns(x, df[x][y]);
    markAns(df[x][y], y);
  }
}
void dfsAns(int x, int y) {
  if ((y & -y) == y || pf[x][y] >= 0) {
    if (pf[x][y] != -1) {
      markAns(x, pf[x][y]);
      dfsAns(pf[x][y], y);
    } else {
      int w = -1;
      for (int i = 0; w < 0; ++i)
        if ((1 << i) == y) w = i;
      markAns(x, u[w]);
    }
  } else {
    dfsAns(x, bf[x][y]);
    dfsAns(x, y - bf[x][y]);
  }
}
inline void readInt(int &v) {
  char c = getchar();
  while (!isdigit(c)) c = getchar();
  for (v = 0; isdigit(c); c = getchar()) v = v * 10 + c - '0';
}
int main() {
  memset(d, 0x3f, sizeof(d));
  memset(df, 0xff, sizeof(df));
  readInt(r);
  readInt(c);
  readInt(m);
  n = r * c;
  for (int i = 0; i < r; ++i)
    for (int j = 0; j < c; ++j, ++t) readInt(d[t][t]);
  for (int i = 0; i < r; ++i)
    for (int j = 0, k; j < c; ++j) {
      k = i * c + j;
      if (j > 0) d[k][k - 1] = d[k][k] + d[k - 1][k - 1];
      if (j + 1 < c) d[k][k + 1] = d[k][k] + d[k + 1][k + 1];
      if (i > 0) d[k][k - c] = d[k][k] + d[k - c][k - c];
      if (i + 1 < r) d[k][k + c] = d[k][k] + d[k + c][k + c];
    }
  for (int i = 0, x, y; i < m; ++i) {
    readInt(x);
    readInt(y);
    u[i] = (x - 1) * c + (y - 1);
  }
  for (int k = 0; k < n; ++k)
    for (int i = 0; i < n; ++i)
      for (int j = 0, w; j < n; ++j) {
        w = d[i][k] + d[k][j] - d[k][k];
        if (w < d[i][j]) {
          d[i][j] = w;
          df[i][j] = k;
        }
      }
  memset(pf, 0xff, sizeof(pf));
  memset(bf, 0xff, sizeof(bf));
  for (int i = 0; i < n; ++i) {
    dp[i][0] = 0;
    for (int j = 0; j < m; ++j) dp[i][1 << j] = d[i][u[j]];
  }
  for (int i = 1, w; i < (1 << m); ++i) {
    if ((i & -i) == i) continue;
    for (int j = 0; j < n; ++j) {
      dp[j][i] = 0x3fffffff;
      for (int k = (i - 1) & i; k > 0; k = (k - 1) & i)
        if ((i | k) == i) {
          w = dp[j][k] + dp[j][i ^ k] - d[j][j];
          if (w < dp[j][i]) {
            dp[j][i] = w;
            bf[j][i] = i ^ k;
          }
        }
    }
    for (int j = 0; j < n; ++j)
      for (int k = 0; k < n; ++k) {
        w = dp[k][i] + d[j][k] - d[k][k];
        if (w < dp[j][i]) {
          dp[j][i] = w;
          pf[j][i] = k;
        }
      }
  }
  int ans = 0x7fffffff;
  for (int i = 0; i < n; ++i)
    if (dp[i][(1 << m) - 1] < ans) {
      ans = dp[i][(1 << m) - 1];
      root = i;
    }
  printf("%d\n", ans);
  dfsAns(root, (1 << m) - 1);
  for (int i = 0, k = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j, ++k) putchar(used[k] ? 'X' : '.');
    putchar('\n');
  }
  return 0;
}
