#include <bits/stdc++.h>
using namespace std;
const int MAXN = 220;
const int INF = 1000000000;
int n, m, k;
int cnt;
int val[MAXN][MAXN], id[MAXN][MAXN];
int pos[MAXN * MAXN][2];
int f[1 << 7][MAXN], path[1 << 7][MAXN][2];
bool inQ[1 << 7][MAXN];
queue<int> Q;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
void Update(int i, int j, int val, int pa, int pb) {
  if (f[i][j] <= val) return;
  f[i][j] = val;
  path[i][j][0] = pa, path[i][j][1] = pb;
  if (!inQ[i][j]) {
    inQ[i][j] = 1;
    Q.push(i);
    Q.push(j);
  }
}
bool in(int x, int y) {
  if (x >= 1 && x <= n && y >= 1 && y <= m) return 1;
  return 0;
}
char mp[MAXN][MAXN];
bool mark[1 << 7][MAXN];
void DFS(int a, int b) {
  if (mark[a][b]) return;
  mark[a][b] = 1;
  int x = pos[b][0], y = pos[b][1];
  mp[x][y] = 'X';
  int pa = path[a][b][0], pb = path[a][b][1];
  if (pb < 0) return;
  if (pb == b) {
    DFS(pa, pb);
    DFS(a - pa, pb);
  } else
    DFS(pa, pb);
}
int main() {
  while (~scanf("%d%d%d", &n, &m, &k)) {
    cnt = 0;
    memset(f, 0x3f, sizeof(f));
    memset(inQ, 0, sizeof(inQ));
    while (!Q.empty()) Q.pop();
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j) {
        scanf("%d", val[i] + j);
        id[i][j] = ++cnt;
        pos[cnt][0] = i;
        pos[cnt][1] = j;
      }
    for (int i = 1; i <= k; ++i) {
      int x, y;
      scanf("%d%d", &x, &y);
      Update(1 << (i - 1), id[x][y], val[x][y], -1, -1);
    }
    while (!Q.empty()) {
      int a = Q.front();
      Q.pop();
      int b = Q.front();
      Q.pop();
      inQ[a][b] = 0;
      for (int i = 0; i < 4; ++i) {
        int nx = pos[b][0] + dx[i], ny = pos[b][1] + dy[i];
        if (!in(nx, ny)) continue;
        Update(a, id[nx][ny], f[a][b] + val[nx][ny], a, b);
      }
      int tot = (1 << k) - 1 - a;
      for (int i = tot; i; i = (i - 1) & tot)
        Update(a | i, b, f[a][b] + f[i][b] - val[pos[b][0]][pos[b][1]], a, b);
    }
    int minx = INF;
    int minj;
    for (int j = 1; j <= cnt; ++j) {
      if (f[(1 << k) - 1][j] < minx) {
        minx = f[(1 << k) - 1][j];
        minj = j;
      }
    }
    printf("%d\n", minx);
    memset(mark, 0, sizeof(mark));
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j) mp[i][j] = '.';
    DFS((1 << k) - 1, minj);
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) printf("%c", mp[i][j]);
      puts("");
    }
  }
  return 0;
}
