#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200 + 50;
const int MAXV = 40000 + 50;
const int MAXQ = 1000000 + 50;
const int sigma_size = 26;
const int inf = 0x3f3f3f3f;
const int INF = ~0U >> 1;
const int IMIN = 0x80000000;
const long long MOD = 1000000000 + 7;
const int mod = 10007;
const double PI = acos(-1.0);
int n, m, k;
queue<int> q;
int bit[MAXN];
int head[MAXN];
int dp[MAXN][1 << 8];
int in[MAXN][1 << 8];
int pre[MAXN][1 << 8];
int val[MAXN];
int vis[MAXN];
int u, v;
struct Edge {
  int v, next;
} e[MAXV];
int num;
void adde(int u, int v) {
  e[num].v = v;
  e[num].next = head[u];
  head[u] = num++;
}
void init() {
  num = 0;
  memset(head, -1, sizeof(head));
  memset(dp, 0x3f, sizeof(dp));
  memset(pre, 0, sizeof(pre));
  memset(bit, 0, sizeof(bit));
  memset(vis, 0, sizeof(vis));
  memset(in, 0, sizeof(in));
  while (!q.empty()) q.pop();
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int cnt = i * m + j;
      scanf("%d", &val[i * m + j]);
      if (i > 0) adde(cnt, cnt - m);
      if (i < n - 1) adde(cnt, cnt + m);
      if (j > 0) adde(cnt, cnt - 1);
      if (j < m - 1) adde(cnt, cnt + 1);
    }
  }
  for (int i = 0; i < k; i++) {
    scanf("%d%d", &u, &v);
    u--, v--;
    int cnt = u * m + v;
    bit[cnt] = 1 << i;
    dp[cnt][bit[cnt]] = val[cnt];
  }
}
void spfa() {
  while (!q.empty()) {
    int x = q.front() & 1023, st = q.front() >> 10;
    in[x][st] = 0;
    q.pop();
    for (int j = head[x]; j != -1; j = e[j].next) {
      int y = e[j].v, nst = st | bit[y];
      if (dp[x][st] + val[y] < dp[y][nst]) {
        dp[y][nst] = dp[x][st] + val[y];
        pre[y][nst] = st << 10 | x;
        if (st == nst && !in[y][nst]) {
          in[y][nst] = 1;
          q.push(nst << 10 | y);
        }
      }
    }
  }
}
void getans(int cnt, int mask) {
  vis[cnt] = 1;
  int tmp = pre[cnt][mask];
  if (!tmp) return;
  int x = tmp & 1023, st = tmp >> 10;
  getans(x, st);
  if (x == cnt) getans(x, (mask - st) | bit[cnt]);
}
int main() {
  init();
  int nn = (1 << k) - 1;
  for (int i = 1; i <= nn; i++) {
    for (int j = 0; j < n * m; j++) {
      if (bit[j] && !(bit[j] & i)) continue;
      for (int k = (i - 1) & i; k; k = (k - 1) & i) {
        if (dp[j][i] > dp[j][k | bit[j]] + dp[j][i - k | bit[j]] - val[j]) {
          dp[j][i] = dp[j][k | bit[j]] + dp[j][i - k | bit[j]] - val[j];
          pre[j][i] = (k | bit[j]) << 10 | j;
        }
      }
      if (dp[j][i] < inf) q.push(i << 10 | j), in[j][i] = 1;
    }
    spfa();
  }
  printf("%d\n", dp[u * m + v][nn]);
  getans(u * m + v, nn);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (vis[i * m + j])
        putchar('X');
      else
        putchar('.');
    }
    puts("");
  }
  return 0;
}
