#include <bits/stdc++.h>
int pq[1 + ((1 << 7) * 200)], iq[((1 << 7) * 200)], dd[((1 << 7) * 200)], cnt;
int less(int u, int v) { return dd[u] < dd[v]; }
int i2(int i) {
  return (i *= 2) > cnt ? 0 : i < cnt && less(pq[i + 1], pq[i]) ? i + 1 : i;
}
void pq_up(int u) {
  int i, j, v;
  for (i = iq[u]; (j = i / 2) && less(u, v = pq[j]); i = j) pq[iq[v] = i] = v;
  pq[iq[u] = i] = u;
}
void pq_dn(int u) {
  int i, j, v;
  for (i = iq[u]; (j = i2(i)) && less(v = pq[j], u); i = j) pq[iq[v] = i] = v;
  pq[iq[u] = i] = u;
}
void pq_add_last(int u) { pq[iq[u] = ++cnt] = u; }
int pq_remove_first() {
  int u = pq[1], v = pq[cnt--];
  iq[v] = 1;
  pq_dn(v);
  return u;
}
void pq_build() {
  int i;
  for (i = cnt / 2; i > 0; i--) pq_dn(pq[i]);
}
int n, m;
int encode(int b, int i, int j) { return (b * n + i) * m + j; }
int bb[1 << 7][100][100], hh[1 << 7][100][100];
int di[] = {-1, 1, 0, 0};
int dj[] = {0, 0, -1, 1};
void update(int b, int i, int j, int a) {
  int h, ab = a | b;
  int c = encode(ab, i, j);
  int d = dd[encode(b, i, j)];
  for (h = 0; h < 4; h++) {
    int i_ = i + di[h], j_ = j + dj[h], c_, d2;
    if (i_ < 0 || i_ >= n || j_ < 0 || j_ >= m) continue;
    c_ = encode(ab, i_, j_);
    d2 = d + dd[encode(a, i_, j_)];
    if (dd[c_] > d2) {
      if (dd[c_] == 0x3f3f3f3f) pq_add_last(c_);
      dd[c_] = d2, bb[ab][i_][j_] = b, hh[ab][i_][j_] = h ^ 1;
      pq_up(c_);
    }
    if (dd[c] > d2) {
      if (dd[c] == 0x3f3f3f3f) pq_add_last(c);
      dd[c] = d2, bb[ab][i][j] = a, hh[ab][i][j] = h;
      pq_up(c);
    }
  }
}
char map[100][100 + 1];
void mark(int b, int i, int j) {
  int a, h;
  if (hh[b][i][j] == -1) {
    map[i][j] = 'X';
    return;
  }
  a = bb[b][i][j], h = hh[b][i][j];
  mark(a, i + di[h], j + dj[h]);
  mark(b ^ a, i, j);
}
int main() {
  static int aa[100][100];
  int k, all, nm, n_, h, i, j;
  scanf("%d%d%d", &n, &m, &k);
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) {
      scanf("%d", &aa[i][j]);
      map[i][j] = '.';
    }
  all = (1 << k) - 1;
  nm = n * m, n_ = (1 << k) * nm;
  memset(dd, 0x3f, n_ * sizeof *dd);
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) {
      int b = 0, c = encode(b, i, j);
      dd[c] = aa[i][j], hh[b][i][j] = -1;
      pq_add_last(c);
    }
  for (h = 0; h < k; h++) {
    int b, c;
    scanf("%d%d", &i, &j), i--, j--;
    b = 1 << h, c = encode(b, i, j);
    dd[c] = aa[i][j], hh[b][i][j] = -1;
    pq_add_last(c);
  }
  pq_build();
  while (cnt) {
    int c = pq_remove_first(), b, b_, a;
    b = c / nm, i = c % nm / m, j = c % m;
    if (b == all) {
      printf("%d\n", dd[c]);
      mark(b, i, j);
      for (i = 0; i < n; i++) printf("%s\n", map[i]);
      return 0;
    }
    a = 0, b_ = b ^ all;
    do update(b, i, j, a);
    while ((a = a - b_ & b_));
  }
  return 0;
}
