#include <bits/stdc++.h>
inline int getint() {
  int x = 0, p = 1;
  char c = getchar();
  while (c <= 32) c = getchar();
  if (c == 45) p = -p, c = getchar();
  while (c > 32) x = x * 10 + c - 48, c = getchar();
  return x * p;
}
using namespace std;
const int S = 7;
const int N = 102;
int n, m, k, a[N][N], ex[S], ey[S], dp[N][N][(1 << S) + 10];
struct st {
  st() {}
  st(int _x, int _y, int _z) {
    x = _x;
    y = _y;
    mask = _z;
  }
  bool operator<(const st) const { return true; }
  int x, y, mask;
};
st pre[N][N][(1 << S) + 10];
bool g[N][N];
priority_queue<pair<int, st>, vector<pair<int, st> >, greater<pair<int, st> > >
    pq;
int dx[] = {1, 0, 0, -1};
int dy[] = {0, 1, -1, 0};
inline bool isvalid(int x, int y) {
  if (x < 0 || x >= n || y < 0 || y >= m) return false;
  return true;
}
void go(int x, int y, int mask) {
  if (x == -1 || !mask) return;
  g[x][y] = true;
  st u = pre[x][y][mask];
  go(u.x, u.y, u.mask);
  if (u.mask != mask && u.mask) go(x, y, u.mask ^ mask);
}
int main() {
  n = getint();
  m = getint();
  k = getint();
  for (int(i) = 0; (i) < (n); (i)++)
    for (int(j) = 0; (j) < (m); (j)++) a[i][j] = getint();
  for (int(i) = 0; (i) < (n); (i)++)
    for (int(j) = 0; (j) < (m); (j)++)
      for (int(k) = 0; (k) < (1 << S); (k)++) dp[i][j][k] = 1000000000;
  for (int(i) = 0; (i) < (k); (i)++) {
    ex[i] = getint() - 1, ey[i] = getint() - 1;
    dp[ex[i]][ey[i]][1 << i] = a[ex[i]][ey[i]];
    pre[ex[i]][ey[i]][1 << i] = st(-1, -1, 0);
    pq.push(make_pair(a[ex[i]][ey[i]], st(ex[i], ey[i], 1 << i)));
  }
  while (!pq.empty()) {
    pair<int, st> uu = pq.top();
    pq.pop();
    st u = uu.second;
    assert(u.mask);
    if (uu.first > dp[u.x][u.y][u.mask]) continue;
    for (int(i) = 0; (i) < (4); (i)++) {
      int fx = u.x + dx[i], fy = u.y + dy[i];
      if (!isvalid(fx, fy)) continue;
      if (dp[u.x][u.y][u.mask] + a[fx][fy] < dp[fx][fy][u.mask]) {
        dp[fx][fy][u.mask] = dp[u.x][u.y][u.mask] + a[fx][fy];
        pre[fx][fy][u.mask] = st(u.x, u.y, u.mask);
        pq.push(make_pair(dp[fx][fy][u.mask], st(fx, fy, u.mask)));
      }
      for (int(msk) = 0; (msk) < (1 << k); (msk)++)
        if (!(msk & u.mask) && msk > 0) {
          if (dp[fx][fy][msk | u.mask] >
              dp[u.x][u.y][u.mask] + dp[fx][fy][msk]) {
            dp[fx][fy][msk | u.mask] = dp[u.x][u.y][u.mask] + dp[fx][fy][msk];
            pre[fx][fy][msk | u.mask] = st(u.x, u.y, u.mask);
            pq.push(
                make_pair(dp[fx][fy][msk | u.mask], st(fx, fy, msk | u.mask)));
          }
        }
    }
  }
  int sx = -1, sy = -1, res = 1000000000;
  for (int(i) = 0; (i) < (n); (i)++)
    for (int(j) = 0; (j) < (m); (j)++)
      if (dp[i][j][(1 << k) - 1] < res) {
        res = dp[i][j][(1 << k) - 1];
        sx = i;
        sy = j;
      }
  printf("%d\n", res);
  go(sx, sy, (1 << k) - 1);
  for (int(i) = 0; (i) < (n); (i)++) {
    for (int(j) = 0; (j) < (m); (j)++)
      if (g[i][j])
        printf("X");
      else
        printf(".");
    printf("\n");
  }
  return 0;
}
