#include <bits/stdc++.h>
using namespace std;
template <class t>
inline t read(t &x) {
  x = 0;
  char c = getchar();
  bool f = 0;
  while (!isdigit(c)) f |= c == '-', c = getchar();
  while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  if (f) x = -x;
  return x;
}
template <class t>
inline void write(t x) {
  if (x < 0) {
    putchar('-'), write(-x);
  } else {
    if (x > 9) write(x / 10);
    putchar('0' + x % 10);
  }
}
const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
const int N = 105, S = (1 << 7) + 5;
int n, m, k, a[N][N], f[N][N][S], ans = 0x3f3f3f3f;
bool bd[N][N], v[N][N][S];
struct node {
  int x, y, s;
} pre[N][N][S];
queue<node> q;
void print(node s) {
  if (pre[s.x][s.y][s.s].s == -1) return;
  if (pre[s.x][s.y][s.s].s == 0) {
    print((node){s.x, s.y, pre[s.x][s.y][s.s].x});
    print((node){s.x, s.y, pre[s.x][s.y][s.s].y});
  } else {
    bd[s.x][s.y] = 1;
    print((node){pre[s.x][s.y][s.s].x, pre[s.x][s.y][s.s].y, s.s});
  }
}
signed main() {
  read(n);
  read(m);
  read(k);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) read(a[i][j]);
  for (int s = 0; s < (1 << k); s++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        f[i][j][s] = 0x3f3f3f3f;
        pre[i][j][s] = (node){-1, -1, -1};
      }
  for (int i = 0, x, y; i < k; i++) {
    read(x);
    read(y);
    f[x][y][1 << i] = a[x][y];
    bd[x][y] = 1;
    q.push((node){x, y, 1 << i});
    v[x][y][1 << i] = 1;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (!bd[i][j]) {
        f[i][j][0] = a[i][j];
        q.push((node){i, j, 0});
        v[i][j][0] = 1;
      }
  while (!q.empty()) {
    node x = q.front();
    q.pop();
    v[x.x][x.y][x.s] = 0;
    for (int s = 1; s < (1 << k); s++)
      if ((s & x.s) == 0) {
        if (f[x.x][x.y][s | x.s] >
            f[x.x][x.y][x.s] + f[x.x][x.y][s] - a[x.x][x.y]) {
          f[x.x][x.y][s | x.s] =
              f[x.x][x.y][x.s] + f[x.x][x.y][s] - a[x.x][x.y];
          pre[x.x][x.y][s | x.s] = (node){x.s, s, 0};
          if (!v[x.x][x.y][x.s | s]) {
            v[x.x][x.y][x.s | s] = 1;
            q.push((node){x.x, x.y, x.s | s});
          }
        }
      }
    for (int i = 0; i < 4; i++) {
      int nx = x.x + dx[i], ny = x.y + dy[i];
      if (nx >= 1 && ny >= 1 && nx <= n && ny <= m) {
        if (f[nx][ny][x.s] > f[x.x][x.y][x.s] + a[nx][ny]) {
          f[nx][ny][x.s] = f[x.x][x.y][x.s] + a[nx][ny];
          pre[nx][ny][x.s] = (node){x.x, x.y, 1};
          v[nx][ny][x.s] = 1;
          q.push((node){nx, ny, x.s});
        }
      }
    }
  }
  node start = (node){0, 0, (1 << k) - 1};
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (f[i][j][(1 << k) - 1] < ans) {
        ans = f[i][j][(1 << k) - 1];
        start = (node){i, j, start.s};
      }
  write(ans);
  print(start);
  for (int i = 1; i <= n; i++) {
    puts("");
    for (int j = 1; j <= m; j++) putchar(bd[i][j] ? 'X' : '.');
  }
}
