#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int di[] = {-1, 1, 0, 0};
const int dj[] = {0, 0, -1, 1};
const int oo = 1000111000;
int a[111][111], m, n;
struct State {
  int mask, u, v;
  State() {}
  State(int u, int v, int mask) : u(u), v(v), mask(mask) {}
};
bool operator<(const State &a, const State &b) {
  if (a.mask != b.mask) return a.mask < b.mask;
  if (a.u != b.u) return a.u < b.u;
  return a.v < b.v;
}
bool outside(int u, int v) {
  if (u < 1 || v < 1 || u > m || v > n) return true;
  return false;
}
set<pair<int, State> > s;
int f[111][111][222], mark[111][111];
State tr[111][111][222], tr2[111][111][222];
char res[111][111];
void trace(State cur) {
  if (cur.mask == 0) return;
  res[cur.u][cur.v] = 'X';
  trace(tr[cur.u][cur.v][cur.mask]);
  if (tr2[cur.u][cur.v][cur.mask].mask >= 0) {
    trace(tr2[cur.u][cur.v][cur.mask]);
  }
}
int main() {
  int k;
  scanf("%d%d%d", &m, &n, &k);
  for (int i = (1), _b = (m); i <= _b; i++)
    for (int j = (1), _b = (n); j <= _b; j++) scanf("%d", &a[i][j]);
  for (int i = (1), _b = (m); i <= _b; i++)
    for (int j = (1), _b = (n); j <= _b; j++)
      for (int mask = 0, _a = ((1 << (k))); mask < _a; mask++)
        f[i][j][mask] = oo;
  for (int i = (1), _b = (k); i <= _b; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    mark[u][v] = i;
    f[u][v][(1 << (i - 1))] = a[u][v];
    s.insert(make_pair(a[u][v], State(u, v, (1 << (i - 1)))));
  }
  while (!s.empty()) {
    State cur = s.begin()->second;
    int l = s.begin()->first;
    s.erase(s.begin());
    if (f[cur.u][cur.v][cur.mask] != l) continue;
    if (cur.mask == (1 << (k)) - 1) {
      printf("%d\n", l);
      memset(res, '.', sizeof res);
      trace(cur);
      for (int i = (1), _b = (m); i <= _b; i++) {
        for (int j = (1), _b = (n); j <= _b; j++) printf("%c", res[i][j]);
        puts("");
      }
      return 0;
    }
    State next;
    for (int dir = 0, _a = (4); dir < _a; dir++) {
      next.u = cur.u + di[dir];
      next.v = cur.v + dj[dir];
      if (outside(next.u, next.v)) continue;
      if (mark[next.u][next.v])
        next.mask = cur.mask | (1 << (mark[next.u][next.v] - 1));
      else
        next.mask = cur.mask;
      int cost = f[cur.u][cur.v][cur.mask] + a[next.u][next.v];
      if (f[next.u][next.v][next.mask] > cost) {
        f[next.u][next.v][next.mask] = cost;
        tr[next.u][next.v][next.mask] = cur;
        tr2[next.u][next.v][next.mask] = State(0, 0, -1);
        s.insert(make_pair(cost, next));
      }
      for (int mask2 = 0, _a = ((1 << (k))); mask2 < _a; mask2++)
        if (mask2)
          if ((mask2 & next.mask) == 0) {
            State savenext = next;
            next.mask = mask2 | next.mask;
            cost = f[cur.u][cur.v][cur.mask] + f[next.u][next.v][mask2];
            if (f[next.u][next.v][next.mask] > cost) {
              f[next.u][next.v][next.mask] = cost;
              s.insert(make_pair(cost, next));
              tr[next.u][next.v][next.mask] = cur;
              tr2[next.u][next.v][next.mask] = State(next.u, next.v, mask2);
            }
            next = savenext;
          }
    }
  }
  return 0;
}
