#include <bits/stdc++.h>
long long MAX(long long x, long long y) { return (x > y) ? x : y; }
long long MIN(long long x, long long y) { return (x < y) ? x : y; }
const int MAXN = 123;
const int MAXNM = 223;
const int MAXK = 7;
const int inf = 0x3f3f3f3f;
using namespace std;
inline int Read() {
  register int x = 0;
  bool flag = false;
  register char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') flag = true;
    ch = getchar();
  }
  if (flag) {
    while ('0' <= ch && ch <= '9') {
      x = x * 10 - ch + '0';
      ch = getchar();
    }
  } else {
    while ('0' <= ch && ch <= '9') {
      x = x * 10 + ch - '0';
      ch = getchar();
    }
  }
  return x;
}
int N, M, K;
int cg(int x, int y) { return (x - 1) * M + y; }
pair<int, int> split(int z) {
  int y = (z - 1) % M + 1;
  return make_pair((z - y) / M + 1, y);
}
int dirx[4] = {1, 0, -1, 0};
int diry[4] = {0, 1, 0, -1};
int trans(int z, int dir) {
  pair<int, int> p = split(z);
  p = make_pair(p.first + dirx[dir], p.second + diry[dir]);
  if (1 <= p.first && p.first <= N && 1 <= p.second && p.second <= M)
    return cg(p.first, p.second);
  else
    return -1;
}
int a[MAXNM];
int f[MAXNM][(1 << MAXK) - 1];
vector<pair<int, int> > frm[MAXNM][(1 << MAXK) - 1];
int ele[MAXK];
queue<int> q;
bool inq[MAXNM];
void spfa(int s) {
  while (!q.empty()) {
    int hd = q.front();
    q.pop();
    inq[hd] = false;
    for (int i = 0; i <= 3; i++) {
      int v = trans(hd, i);
      if (~v) {
        if (f[v][s] > f[hd][s] + a[v]) {
          f[v][s] = f[hd][s] + a[v];
          frm[v][s].clear();
          frm[v][s].push_back(make_pair(hd, s));
          if (!inq[v]) {
            q.push(v);
            inq[v] = true;
          }
        }
      }
    }
  }
}
char mp[MAXN][MAXN];
void print(int i, int s) {
  pair<int, int> p = split(i);
  mp[p.first][p.second] = 'X';
  for (auto &v : frm[i][s]) print(v.first, v.second);
}
int main() {
  memset(f, 0x3f, sizeof(f));
  N = Read();
  M = Read();
  K = Read();
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= M; j++) mp[i][j] = '.';
  for (int i = 1; i <= N * M; i++) a[i] = Read();
  for (int i = 0; i <= K - 1; i++) {
    int x = Read(), y = Read();
    ele[i] = cg(x, y);
    f[ele[i]][1 << i] = a[ele[i]];
  }
  for (int s = 1; s <= (1 << K) - 1; s++) {
    for (int ss = s & (s - 1); ss; ss = s & (ss - 1))
      for (int i = 1; i <= N * M; i++)
        if (f[i][s] > f[i][ss] + f[i][s ^ ss] - a[i]) {
          f[i][s] = f[i][ss] + f[i][s ^ ss] - a[i];
          frm[i][s].clear();
          frm[i][s].push_back(make_pair(i, ss));
          frm[i][s].push_back(make_pair(i, s ^ ss));
        }
    for (int i = 1; i <= N * M; i++)
      if (f[i][s] < inf) {
        q.push(i);
        inq[i] = true;
      }
    spfa(s);
  }
  int ans = inf, id;
  for (int i = 1; i <= N * M; i++)
    if (f[i][(1 << K) - 1] < ans) ans = f[id = i][(1 << K) - 1];
  printf("%d\n", ans);
  print(id, (1 << K) - 1);
  for (int i = 1; i <= N; i++) printf("%s\n", mp[i] + 1);
  return 0;
}
