#include <bits/stdc++.h>
using namespace std;
template <class T>
T** grid_alloc(int y, int x) {
  T** rv = new T*[y];
  for (int i = 0; i < y; i++) rv[i] = new T[x];
  return rv;
}
template <class T>
void grid_free(T** ptr, int y, int x) {
  for (int i = 0; i < y; i++) delete[] ptr[i];
  delete[] ptr;
}
void grid_print(int** ptr, int y, int x) {
  for (int i = 0; i < y; i++) {
    for (int j = 0; j < x; j++) printf("%d ", ptr[i][j]);
    printf("\n");
  }
}
int N, M, K;
int** flowers;
bool** pavement;
int points[10][2];
void shortest_path(int** sp_cost, int** sp_parent, int cy, int cx) {
  for (int y = 0; y < N; y++)
    for (int x = 0; x < M; x++) {
      sp_cost[y][x] = 2000000000;
      sp_parent[y][x] = -1;
    }
  int c = cy * M + cx, p = 0, cost = 0, ctotal = 0;
  priority_queue<pair<int, pair<int, int> >,
                 vector<pair<int, pair<int, int> > >,
                 greater<pair<int, pair<int, int> > > >
      q;
  q.push(make_pair(0, make_pair(c, c)));
  int offset_y[4] = {-1, 0, 1, 0};
  int offset_x[4] = {0, 1, 0, -1};
  while (!q.empty() && ctotal < N * M) {
    cost = q.top().first;
    c = q.top().second.first;
    p = q.top().second.second;
    q.pop();
    cy = c / M;
    cx = c % M;
    if (sp_cost[cy][cx] != 2000000000) continue;
    sp_cost[cy][cx] = cost;
    sp_parent[cy][cx] = p;
    ctotal++;
    for (int i = 0; i < 4; i++) {
      int ny = cy + offset_y[i];
      int nx = cx + offset_x[i];
      if (ny >= 0 && ny < N && nx >= 0 && nx < M &&
          sp_cost[ny][nx] == 2000000000) {
        int ncost = cost + flowers[ny][nx];
        if (ncost < sp_cost[ny][nx])
          q.push(make_pair(ncost, make_pair(ny * M + nx, c)));
      }
    }
  }
}
int*** path_cost;
int*** path_parent;
int bitcount(int a) {
  int r = 0;
  while (a > 0) {
    if (a & 0x1) r++;
    a >>= 1;
  }
  return r;
}
int mask_normalize(int mask, int value) {
  int i = 0;
  int r = 0;
  while (mask > 0) {
    if (mask & 0x1) {
      r |= (value & 0x1) << i;
      value >>= 1;
    }
    mask >>= 1;
    i++;
  }
  return r;
}
int mem[130][250];
int rtrace_c;
int rtrace_m1;
int rtrace_m2;
int solve(int mask, int c, bool backtrace) {
  if (!backtrace && mem[mask][c] >= 0) return mem[mask][c];
  int y = c / M;
  int x = c % M;
  int rtc_c = -1;
  int rtc_m1 = 0;
  int rtc_m2 = 0;
  int** sp_cost = path_cost[c];
  int** sp_parent = path_parent[c];
  int rv = 2000000000;
  int bc = bitcount(mask);
  int ty = 0, tx = 0;
  if (bc == 1) {
    int point_i = 0, maskc = mask;
    while (true) {
      if (maskc & 0x1) break;
      maskc >>= 1;
      point_i++;
    }
    ty = points[point_i][0];
    tx = points[point_i][1];
    rv = sp_cost[ty][tx];
  } else {
    int mask_max = 0x1 << (bc - 1);
    for (int sub = 1; sub < mask_max; sub++) {
      int mask1 = mask_normalize(mask, sub);
      int mask2 = mask_normalize(mask, ~sub);
      for (int ny = 0; ny < N; ny++)
        for (int nx = 0; nx < M; nx++) {
          int cr = sp_cost[ny][nx];
          int cc = ny * M + nx;
          cr += solve(mask1, cc, false);
          cr += solve(mask2, cc, false);
          if (cr < rv) {
            rv = cr;
            ty = ny;
            tx = nx;
            rtc_c = cc;
            rtc_m1 = mask1;
            rtc_m2 = mask2;
          }
        }
    }
  }
  if (backtrace) {
    int tc = ty * M + tx;
    while (sp_parent[ty][tx] != tc) {
      pavement[ty][tx] = true;
      tc = sp_parent[ty][tx];
      ty = tc / M;
      tx = tc % M;
    }
  }
  rtrace_c = rtc_c;
  rtrace_m1 = rtc_m1;
  rtrace_m2 = rtc_m2;
  return mem[mask][c] = rv;
}
void trace(int mask, int c) {
  solve(mask, c, true);
  int rtc_c = rtrace_c;
  int rtc_m1 = rtrace_m1;
  int rtc_m2 = rtrace_m2;
  if (rtc_c >= 0) {
    trace(rtc_m1, rtc_c);
    trace(rtc_m2, rtc_c);
  }
}
int main() {
  for (int i = 0; i < 130; i++)
    for (int j = 0; j < 250; j++) mem[i][j] = -1;
  scanf("%d %d %d", &N, &M, &K);
  flowers = grid_alloc<int>(N, M);
  pavement = grid_alloc<bool>(N, M);
  for (int y = 0; y < N; y++)
    for (int x = 0; x < M; x++) {
      scanf("%d", &flowers[y][x]);
      pavement[y][x] = false;
    }
  for (int i = 0; i < K; i++) {
    scanf("%d %d", &points[i][0], &points[i][1]);
    points[i][0]--;
    points[i][1]--;
  }
  path_cost = new int**[N * M];
  path_parent = new int**[N * M];
  for (int y = 0; y < N; y++)
    for (int x = 0; x < M; x++) {
      int c = y * M + x;
      path_cost[c] = grid_alloc<int>(N, M);
      path_parent[c] = grid_alloc<int>(N, M);
      shortest_path(path_cost[c], path_parent[c], y, x);
    }
  int sy = points[0][0];
  int sx = points[0][1];
  solve((0x1 << K) - 1, sy * M + sx, false);
  pavement[sy][sx] = true;
  trace((0x1 << K) - 1, sy * M + sx);
  int rv = 0;
  for (int y = 0; y < N; y++)
    for (int x = 0; x < M; x++)
      if (pavement[y][x]) rv += flowers[y][x];
  printf("%d\n", rv);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) printf("%c", pavement[i][j] ? 'X' : '.');
    printf("\n");
  }
  for (int c = 0; c < N * M; c++) {
    grid_free<int>(path_cost[c], N, M);
    grid_free<int>(path_parent[c], N, M);
  }
  delete[] path_cost;
  delete[] path_parent;
  grid_free<int>(flowers, N, M);
  grid_free<bool>(pavement, N, M);
}
