#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
int f[201][201][201], px[201][201][201], py[201][201][201], x1[201][201][201];
int x2[201][201][201], dat[201][201], n, m, k;
int qx[3000000], qy[3000000], qz[3000000], l, r;
bool used[300][300];
void search(int x, int y, int z) {
  used[x][y] = true;
  if (x == 0 || y == 0) return;
  if (x1[x][y][z] == 0) {
    search(px[x][y][z], py[x][y][z], z);
  } else {
    search(px[x][y][z], py[x][y][z], x1[x][y][z]);
    search(px[x][y][z], py[x][y][z], x2[x][y][z]);
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) scanf("%d", &dat[i][j]);
  memset(f, 50, sizeof(f));
  for (int i = 1; i <= k; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    qx[++r] = x;
    qy[r] = y;
    qz[r] = (1 << (i - 1));
    f[x][y][1 << (i - 1)] = dat[x][y];
  }
  while (l < r) {
    ++l;
    int x = qx[l], y = qy[l], z = qz[l];
    for (int i = 1; i < (1 << k); ++i) {
      if (f[x][y][z] + f[x][y][i] - dat[x][y] < f[x][y][z | i]) {
        f[x][y][z | i] = f[x][y][z] + f[x][y][i] - dat[x][y];
        qx[++r] = x;
        qy[r] = y;
        qz[r] = z | i;
        px[x][y][z | i] = x;
        py[x][y][z | i] = y;
        x1[x][y][z | i] = z;
        x2[x][y][z | i] = i;
      }
    }
    for (int i = 0; i < 4; ++i) {
      int xx = x + dx[i], yy = y + dy[i];
      if (xx > 0 && xx <= n && yy > 0 && yy <= m &&
          f[x][y][z] + dat[xx][yy] < f[xx][yy][z]) {
        qx[++r] = xx;
        qy[r] = yy;
        qz[r] = z;
        f[xx][yy][z] = f[x][y][z] + dat[xx][yy];
        px[xx][yy][z] = x;
        py[xx][yy][z] = y;
        x1[xx][yy][z] = 0;
        x2[xx][yy][z] = 0;
      }
    }
  }
  int maxx = 1000000000, xxx, yyy;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (f[i][j][(1 << k) - 1] < maxx)
        maxx = f[i][j][(1 << k) - 1], xxx = i, yyy = j;
  search(xxx, yyy, (1 << k) - 1);
  cout << maxx << endl;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j)
      if (used[i][j])
        printf("X");
      else
        printf(".");
    cout << endl;
  }
  return 0;
}
