#include <bits/stdc++.h>
int f[101][101][1101], n, m, k, pre[101][101][1101][3], map[101][101], cnt,
    queue[2000001][2], h, t, rx, ry;
bool ans[101][101], vis[101][101], vv[101][101];
void push(int i, int j) {
  if (vis[i][j]) return;
  queue[++t][0] = i;
  queue[t][1] = j;
  vis[i][j] = 1;
}
void update(int i, int j, int ni, int nj, int now) {
  if (!i || !j || i > n || j > m) return;
  vis[ni][nj] = 0;
  if (f[i][j][now] > f[ni][nj][now] + map[i][j]) {
    f[i][j][now] = f[ni][nj][now] + map[i][j];
    pre[i][j][now][0] = ni;
    pre[i][j][now][1] = nj;
    pre[i][j][now][2] = now;
    push(i, j);
  }
}
void spfa(int now) {
  while (h < t) {
    h++;
    int i = queue[h][0], j = queue[h][1];
    update(i - 1, j, i, j, now);
    update(i + 1, j, i, j, now);
    update(i, j - 1, i, j, now);
    update(i, j + 1, i, j, now);
  }
  h = t = 0;
}
void dfs(int i, int j, int stat) {
  if (!i) return;
  ans[i][j] = 1;
  dfs(pre[i][j][stat][0], pre[i][j][stat][1], pre[i][j][stat][2]);
  if (pre[i][j][stat][0] == i && pre[i][j][stat][1] == j)
    dfs(pre[i][j][stat][0], pre[i][j][stat][1], stat ^ pre[i][j][stat][2]);
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  memset(f, 0x7f7f7f7f, sizeof f);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      scanf("%d", &map[i][j]);
    }
  for (int i = 1, t1, t2; i <= k; i++) {
    scanf("%d%d", &t1, &t2);
    f[t1][t2][1 << (i - 1)] = map[t1][t2];
    rx = t1, ry = t2;
    vv[t1][t2] = 1;
  }
  for (int now = 1; now < (1 << k); now++) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        for (int sub = now & (now - 1); sub; sub = now & (sub - 1))
          if (f[i][j][now] > f[i][j][sub] + f[i][j][now ^ sub] - map[i][j]) {
            f[i][j][now] = f[i][j][sub] + f[i][j][now ^ sub] - map[i][j];
            pre[i][j][now][0] = i;
            pre[i][j][now][1] = j;
            pre[i][j][now][2] = sub;
          }
        if (f[i][j][now] != 0x7f7f7f7f) push(i, j);
      }
    spfa(now);
  }
  dfs(rx, ry, (1 << k) - 1);
  printf("%d\n", f[rx][ry][(1 << k) - 1]);
  for (int i = 1; i <= n; i++, putchar('\n'))
    for (int j = 1; j <= m; j++)
      if (vv[i][j])
        putchar('X');
      else if (ans[i][j])
        putchar('X');
      else
        putchar('.');
}
