#include <bits/stdc++.h>
int n, m, k, p, a[7], c[205][205], z[205][205], d[128][205], h[128][205],
    f[128][205], A = 1 << 29, R;
char o[205];
void fp(int i, int j) {
  int t = z[i][j];
  if (t < 0) return;
  o[t] = 'X';
  fp(i, t);
  fp(t, j);
}
void trace(int i, int j) {
  o[j] = 'X';
  if (h[i][j] >= 0) {
    trace(h[i][j], j);
    trace(i ^ h[i][j], j);
  }
  if (f[i][j] >= 0 && f[i][j] != j) {
    fp(j, f[i][j]);
    trace(i, f[i][j]);
  }
  return;
}
int main() {
  scanf("%d %d %d", &n, &m, &k);
  for (int i = (0); i < (n * m); i++)
    for (int j = (0); j < (n * m); j++) c[i][j] = 1 << 29, z[i][j] = -1;
  for (int i = (0); i < (1 << k); i++)
    for (int j = (0); j < (n * m); j++)
      d[i][j] = 1 << 29, h[i][j] = f[i][j] = -1;
  for (int i = (0); i < (n * m); i++) o[i] = '.';
  for (int i = (0); i < (n); i++)
    for (int j = (0); j < (m); j++) {
      scanf("%d", &c[p][p]);
      p++;
    }
  p = 0;
  for (int i = (0); i < (n); i++)
    for (int j = (0); j < (m); j++) {
      if (j < m - 1) c[p][p + 1] = c[p + 1][p] = c[p][p] + c[p + 1][p + 1];
      if (i < n - 1) c[p][p + m] = c[p + m][p] = c[p][p] + c[p + m][p + m];
      p++;
    }
  for (int t = (0); t < (p); t++)
    for (int i = (0); i < (p); i++)
      for (int j = (i + 1); j < (p); j++)
        if (c[i][j] > c[i][t] + c[t][j] - c[t][t])
          c[i][j] = c[j][i] = c[i][t] + c[t][j] - c[t][t],
          z[i][j] = z[j][i] = t;
  for (int i = (0); i < (k); i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    a[i] = (x - 1) * m + y - 1;
    for (int j = (0); j < (p); j++)
      d[1 << i][j] = c[a[i]][j], f[1 << i][j] = a[i];
  }
  for (int i = (3); i < (1 << k); i++) {
    if ((i & -i) == i) continue;
    for (int j = (0); j < (p); j++)
      for (int t = i - 1 & i; t; t = t - 1 & i)
        if (d[i][j] > d[t][j] + d[t ^ i][j] - c[j][j])
          d[i][j] = d[t][j] + d[t ^ i][j] - c[j][j], h[i][j] = t;
    for (int j = (0); j < (p); j++)
      for (int t = (0); t < (p); t++)
        if (d[i][j] > d[i][t] + c[t][j] - c[t][t])
          d[i][j] = d[i][t] + c[t][j] - c[t][t], f[i][j] = t, h[i][j] = -1;
  }
  printf("%d\n", d[(1 << k) - 1][a[0]]);
  trace((1 << k) - 1, a[0]);
  for (int i = (0); i < (p); i++) {
    putchar(o[i]);
    if (i % m == m - 1) putchar('\n');
  }
  scanf("\n");
}
