#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000;
char ch;
bool v[111][111];
struct data {
  int x1, y1, s1, x2, y2, s2;
  void sv(int a, int b, int c, int d, int e, int f) {
    x1 = a;
    y1 = b;
    s1 = c;
    x2 = d;
    y2 = e;
    s2 = f;
  }
} p[111][111][260];
int i, j, k, n, m, f[111][111][260], a[111][111], lim, c[111][111], ans, t,
    q[2000001][2], l, r, x, y, num, ax, ay;
inline void update(int tx, int ty) {
  if (tx < 1 || tx > n || ty < 1 || ty > m) return;
  if (f[tx][ty][i | a[tx][ty]] > f[x][y][i] + c[x][y]) {
    f[tx][ty][i | a[tx][ty]] = f[x][y][i] + c[x][y];
    p[tx][ty][i | a[tx][ty]].sv(x, y, i, 0, 0, 0);
    if (!v[tx][ty]) v[tx][ty] = 1, q[++r][0] = tx, q[r][1] = ty;
  }
}
void dfs(int x, int y, int s) {
  if (!x) return;
  data w = p[x][y][s];
  v[x][y] = 1;
  dfs(w.x1, w.y1, w.s1);
  dfs(w.x2, w.y2, w.s2);
}
int main() {
  scanf("%d%d%d", &n, &m, &num);
  lim = (1 << num);
  l = r = 0;
  memset(v, 0, sizeof(v));
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) {
      scanf("%d", &c[i][j]);
      a[i][j] = 0;
      for (k = 0; k < lim; k++) f[i][j][k] = inf;
    }
  for (i = 1; i <= num; i++) {
    scanf("%d%d", &j, &k);
    a[j][k] = (1 << (i - 1));
    q[++r][0] = j;
    q[r][1] = k;
    v[j][k] = 1;
    f[j][k][a[j][k]] = 0;
    p[j][k][a[j][k]].sv(0, 0, 0, 0, 0, 0);
  }
  while (l < r) {
    x = q[++l][0];
    y = q[l][1];
    v[x][y] = 0;
    for (i = 0; i < lim; i++)
      for (j = 0; j < lim; j++)
        if (f[x][y][i | j] > f[x][y][i] + f[x][y][j]) {
          f[x][y][i | j] = f[x][y][i] + f[x][y][j];
          p[x][y][i | j].sv(x, y, i, x, y, j);
        }
    for (i = 0; i < lim; i++) {
      if (f[x][y][i] >= inf) continue;
      update(x - 1, y);
      update(x + 1, y);
      update(x, y - 1);
      update(x, y + 1);
    }
  }
  for (i = 1, ans = inf; i <= n; i++)
    for (j = 1; j <= m; j++)
      if (ans > f[i][j][lim - 1] + c[i][j])
        ans = f[i][j][lim - 1] + c[i][j], ax = i, ay = j;
  memset(v, 0, sizeof(v));
  dfs(ax, ay, lim - 1);
  printf("%d\n", ans);
  for (i = 1; i <= n; i++, puts(""))
    for (j = 1; j <= m; j++) putchar(v[i][j] ? 'X' : '.');
  return 0;
}
