#include <bits/stdc++.h>
using namespace std;
int r, c, m, p, q, w[128][200], a[200], u[256], in[200];
pair<int, int> o[128][200];
vector<int> e[200];
char ans[100][105];
void visit(int s, int x) {
  ans[x / c][x % c] = 'X';
  int t, y;
  tie(t, y) = o[s][x];
  if (t) visit(t, y);
  if (t && t != s) visit(s ^ t, y);
}
int main() {
  scanf("%d%d%d", &r, &c, &m);
  int mask = (1 << m) - 1, n = r * c;
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) {
      int x = i * c + j;
      scanf("%d", a + x);
      if (i) e[x].push_back(x - c);
      if (j) e[x].push_back(x - 1);
      if (i + 1 < r) e[x].push_back(x + c);
      if (j + 1 < c) e[x].push_back(x + 1);
      ans[i][j] = '.';
    }
  memset(w, 63, sizeof(w));
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &p, &q);
    int x = --p * c + --q;
    w[1 << i][x] = a[x];
  }
  for (int x, s = 1; s <= mask; s++) {
    for (x = m = 0; x < n; in[u[m++] = x++] = 1)
      for (p = (s - 1) & s; p; p = (p - 1) & s) {
        int ok = w[p][x] + w[s ^ p][x] - a[x];
        if (ok >= w[s][x]) continue;
        w[s][x] = ok;
        o[s][x] = {p, x};
      }
    for (int i = 0; i < m; i++) {
      in[x = u[i & 255]] = 0;
      for (auto y : e[x]) {
        int ok = w[s][x] + a[y];
        if (ok >= w[s][y]) continue;
        w[s][y] = ok;
        o[s][y] = {s, x};
        if (!in[y]) in[u[m++ & 255] = y] = 1;
      }
    }
  }
  int x = min_element(w[mask], w[mask] + n) - w[mask];
  visit(mask, x);
  printf("%d\n", w[mask][x]);
  for (int i = 0; i < r; i++) puts(ans[i]);
}
