#include <bits/stdc++.h>
int i, j, k, n, m, a[128][128], z, x, y, t, s, l, b, A, v[128][128],
    f[256][128], g[256][128], X[] = {0, 1, 0, -1}, Y[] = {1, 0, -1, 0};
char u[128][128];
char p[] = "0123456789abcdefghijklmnopqrstuvwxyz";
void dfs(int x, int y, int d) {
  for (int i = 0; i < 4; ++i) {
    t = x + X[i];
    s = y + Y[i];
    if (t >= 0 && s >= 0 && t < n && s < m)
      if (d + a[t][s] < f[t * m + s][z]) {
        f[t * m + s][z] = d + a[t][s];
        g[t * m + s][z] = 0;
        dfs(t, s, d + a[t][s]);
      }
  }
}
void ff(int x, int y, int z) {
  u[x][y] = 'X';
  if (g[x * m + y][z] == 0) {
    int r = f[x * m + y][z] - a[x][y];
    if (r) {
      for (int i = 0; i < 4; ++i) {
        t = x + X[i];
        s = y + Y[i];
        if (t >= 0 && s >= 0 && t < n && s < m)
          if (f[t * m + s][z] == r) {
            ff(t, s, z);
            break;
          }
      }
    }
  } else {
    int w = g[x * m + y][z];
    for (int i = 0; i < 4; ++i) {
      t = x + X[i];
      s = y + Y[i];
      if (t >= 0 && s >= 0 && t < n && s < m)
        if (f[t * m + s][w] + f[x * m + y][z ^ w] == f[x * m + y][z]) {
          ff(t, s, w);
          ff(x, y, z ^ w);
          break;
        }
    }
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (; i < n; ++i) {
    memset(u[i], '.', m);
    for (j = 0; j < m; ++j) scanf("%d", a[i] + j);
  }
  memset(f, 32, sizeof(f));
  memset(v, 255, sizeof(v));
  for (i = 0; i < k; ++i) {
    scanf("%d%d", &x, &y);
    --x;
    --y;
    z = 1 << i;
    v[x][y] = i;
    dfs(x, y, f[x * m + y][z] = a[x][y]);
  }
  for (z = 3; z < (1 << k); ++z)
    if (__builtin_popcount(z) > 1)
      for (x = z - 1; x; x = (x - 1) & z) {
        y = x ^ z;
        for (i = A = 0; i < n; ++i)
          for (j = 0; j < m; ++A, ++j)
            for (l = 0; l < 4; ++l) {
              t = i + X[l];
              s = j + Y[l];
              if (t >= 0 && s >= 0 && t < n && s < m) {
                b = t * m + s;
                if (f[A][x] + f[b][y] < f[A][z]) {
                  f[A][z] = f[A][x] + f[b][y];
                  g[A][z] = y;
                }
              }
            }
        for (y = 16; y--;)
          for (i = A = 0; i < n; ++i)
            for (j = 0; j < m; ++A, ++j)
              for (l = 0; l < 4; ++l) {
                t = i + X[l];
                s = j + Y[l];
                if (t >= 0 && s >= 0 && t < n && s < m)
                  if (!~v[t][s]) {
                    b = t * m + s;
                    if (f[A][z] + a[t][s] < f[b][z]) {
                      f[b][z] = f[A][z] + a[t][s];
                      g[b][z] = 0;
                    }
                  }
              }
      }
  for (z = (1 << k) - 1, i = n * m, j = 0; --i;)
    if (f[i][z] < f[j][z]) j = i;
  printf("%d\n", f[j][z]);
  ff(j / m, j % m, z);
  for (i = 0; i < n; ++i) puts(u[i]);
  return 0;
}
