#include <bits/stdc++.h>
using namespace std;
int n, m, k, tot;
struct Node {
  int x, y, s, c;
  Node(int X, int Y, int S, int C) {
    x = X;
    y = Y;
    s = S;
    c = C;
  }
  Node() {}
  friend bool operator<(const Node& A, const Node& B) { return A.c > B.c; };
};
const int mm = 107;
const int go[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int mp[mm][mm], mk[mm][mm];
int f[130][mm][mm], v[130][mm][mm];
pair<int, int> q[10];
int X, Y;
void solve() {
  tot = 10000013 * 10;
  memset(f, 127, sizeof(f));
  memset(v, 0, sizeof(v));
  priority_queue<Node> h;
  while (!h.empty()) h.pop();
  int b = 1 << k;
  for (int i = 0; i < k; ++i) {
    f[1 << i][q[i].first][q[i].second] = 0;
    h.push(Node(q[i].first, q[i].second, 1 << i, 0));
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) h.push(Node(i, j, 0, 0)), f[0][i][j] = 0;
  }
  while (!h.empty()) {
    Node t = h.top();
    h.pop();
    if (v[t.s][t.x][t.y]) continue;
    v[t.s][t.x][t.y] = 1;
    if (t.s == b - 1 && t.c + mp[t.x][t.y] < tot) {
      tot = t.c + mp[t.x][t.y];
      X = t.x, Y = t.y;
    }
    for (int i = 0; i < 4; ++i) {
      int tx = t.x + go[i][0], ty = t.y + go[i][1];
      if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
      if (t.c + mp[t.x][t.y] < f[t.s][tx][ty]) {
        f[t.s][tx][ty] = t.c + mp[t.x][t.y];
        h.push(Node(tx, ty, t.s, f[t.s][tx][ty]));
      }
    }
    for (int p = (b - 1) ^ t.s, ps = p; ps; ps = (ps - 1) & p) {
      if (v[ps][t.x][t.y]) {
        int ss = t.s | ps;
        int val = t.c + f[ps][t.x][t.y];
        if (val < f[ss][t.x][t.y]) {
          f[ss][t.x][t.y] = val;
          h.push(Node(t.x, t.y, ss, val));
        }
      }
    }
  }
}
void recover(int x, int y, int b) {
  mk[x][y] = 1;
  for (int s = 1; s < b; ++s) {
    if ((b | s) != b) continue;
    if (f[s][x][y] + f[b - s][x][y] == f[b][x][y]) {
      recover(x, y, s);
      recover(x, y, b - s);
      return;
    }
  }
  for (int i = 0; i < 4; ++i) {
    int tx = x + go[i][0], ty = y + go[i][1];
    if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
    if (f[b][tx][ty] + mp[tx][ty] == f[b][x][y]) {
      recover(tx, ty, b);
      return;
    }
  }
}
int main() {
  int T = 1;
  for (int ci = 1; ci <= T; ++ci) {
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        int x;
        scanf("%d", &x);
        mp[i][j] = x;
      }
    }
    for (int i = 0; i < k; ++i) {
      int x, y;
      scanf("%d%d", &x, &y);
      q[i] = pair<int, int>(x, y);
    }
    solve();
    cout << tot << endl;
    memset(mk, 0, sizeof(mk));
    recover(X, Y, (1 << k) - 1);
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        printf("%c", mk[i][j] ? 'X' : '.');
      }
      puts("");
    }
  }
  return 0;
}
