#include <bits/stdc++.h>
using namespace std;
int n, m, k, best = INT_MAX, now, x[7], y[7], a[100][100], f[1 << 7][100][100],
             path[1 << 7][100][100][3];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
inline void update(int mask) {
  queue<pair<int, int> > q;
  bool mark[n][m];
  memset(mark, false, sizeof(mark));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (f[mask][i][j] < 1000000000) {
        q.push(make_pair(i, j));
        mark[i][j] = true;
      }
  for (; !q.empty(); q.pop()) {
    pair<int, int> head = q.front();
    for (int i = 0; i < 4; i++) {
      pair<int, int> next;
      next.first = head.first + dx[i], next.second = head.second + dy[i];
      if (next.first >= 0 && next.first < n && next.second >= 0 &&
          next.second < m) {
        int tmp = f[mask][head.first][head.second] + a[next.first][next.second];
        if (tmp < f[mask][next.first][next.second]) {
          f[mask][next.first][next.second] = tmp;
          path[mask][next.first][next.second][0] = 0;
          path[mask][next.first][next.second][1] = head.first;
          path[mask][next.first][next.second][2] = head.second;
          if (!mark[next.first][next.second]) {
            q.push(next);
            mark[next.first][next.second] = true;
          }
        }
      }
    }
    mark[head.first][head.second] = false;
  }
}
void modify(int x, int y, int mask) {
  a[x][y] = 0;
  if (path[mask][x][y][0] == 0)
    modify(path[mask][x][y][1], path[mask][x][y][2], mask);
  if (path[mask][x][y][0] == 1)
    modify(x, y, path[mask][x][y][1]), modify(x, y, path[mask][x][y][2]);
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      scanf("%d", &a[i][j]);
      for (int mask = 0; mask < 1 << k; mask++) f[mask][i][j] = 1000000000;
    }
  for (int i = 0; i < k; i++) {
    scanf("%d%d", &x[i], &y[i]);
    x[i]--;
    y[i]--;
    f[1 << i][x[i]][y[i]] = a[x[i]][y[i]];
    path[1 << i][x[i]][y[i]][0] = 2;
  }
  for (int mask = 1; mask < 1 << k; mask++) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        for (int submask = mask; submask > 0; submask = mask & (submask - 1)) {
          int tmp = f[submask][i][j] + f[mask ^ submask][i][j] - a[i][j];
          if (tmp < f[mask][i][j]) {
            f[mask][i][j] = tmp;
            path[mask][i][j][0] = 1;
            path[mask][i][j][1] = submask;
            path[mask][i][j][2] = mask ^ submask;
          }
        }
    update(mask);
  }
  int sx, sy;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (f[(1 << k) - 1][i][j] < best) {
        best = f[(1 << k) - 1][i][j];
        sx = i, sy = j;
      }
  modify(sx, sy, (1 << k) - 1);
  printf("%d\n", best);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      if (a[i][j] > 0)
        printf(".");
      else
        printf("X");
    printf("\n");
  }
  return 0;
}
