#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const long long MOD = 1000000007;
const long long INF = 0x3f3f3f3f;
int val[111][111];
int n, m, k;
int used[111][111];
pair<int, int> pre[211][1 << 8];
int dp[211][1 << 8];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
void doit(int p, int sta) {
  int u = (p - 1) / m + 1, v = (p - 1) % m + 1;
  used[u][v] = 1;
  if (pre[p][sta].first == -1) return;
  doit(pre[p][sta].first, pre[p][sta].second);
  doit(p, sta - pre[p][sta].second);
}
int main() {
  int i, j, l, ll, u, v, w, st, pt;
  int x, y, p, q;
  while (scanf("%d%d%d", &n, &m, &k) != EOF) {
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        scanf("%d", &val[i][j]);
      }
    }
    for (i = 0; i < 211; i++)
      for (j = 0; j < 1 << 8; j++) pre[i][j] = make_pair(-1, -1);
    for (u = 1; u <= n; u++) {
      for (v = 1; v <= m; v++) {
        i = (u - 1) * m + v;
        for (j = 0; j < 1 << k; j++) {
          dp[i][j] = MOD;
        }
      }
    }
    for (i = 0; i < k; i++) {
      scanf("%d%d", &u, &v);
      dp[(u - 1) * m + v][1 << i] = val[u][v];
    }
    for (j = 1; j <= n * m; j++) {
      int flag = false;
      for (u = 1; u <= n; u++) {
        for (v = 1; v <= m; v++) {
          p = (u - 1) * m + v;
          for (i = 0; i < 4; i++) {
            x = u + dx[i];
            y = v + dy[i];
            if (x < 1 || x > n || y < 1 || y > m) continue;
            q = (x - 1) * m + y;
            for (l = 0; l < (1 << k); l++) {
              if (dp[q][l] > dp[p][l] + val[x][y]) {
                dp[q][l] = dp[p][l] + val[x][y];
                pre[q][l] = make_pair(p, l);
              }
              for (ll = 0; ll < (1 << k); ll++) {
                if (ll & l) continue;
                if (dp[q][ll | l] > dp[p][l] + dp[q][ll]) {
                  dp[q][ll | l] = dp[p][l] + dp[q][ll];
                  pre[q][ll | l] = make_pair(p, l);
                  flag = true;
                }
              }
            }
          }
        }
      }
      if (!flag) break;
    }
    int ans = MOD;
    for (i = 1; i <= n; i++)
      for (j = 1; j <= m; j++) used[i][j] = 0;
    for (i = 1; i <= n * m; i++) {
      if (ans > dp[i][((1 << k) - 1)]) {
        ans = dp[i][((1 << k) - 1)];
        p = i;
      }
    }
    doit(p, ((1 << k) - 1));
    printf("%d\n", ans);
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        if (used[i][j])
          putchar('X');
        else
          putchar('.');
      }
      puts("");
    }
  }
  return 0;
}
