#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
const int K = (1 << 10) + 10;
const int inf = 0x3f3f3f3f;
struct Edge {
  int to, nxt;
} e[N << 2];
queue<int> q;
pair<int, int> pre[N][K];
int n, m, k, tot, head[N], a[N], ans[N], vis[N], f[N][K];
void Build(int x, int y) {
  e[++tot].to = y;
  e[tot].nxt = head[x];
  head[x] = tot;
  e[++tot].to = x;
  e[tot].nxt = head[y];
  head[y] = tot;
}
void bfs(int S) {
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    vis[x] = 0;
    for (int i = head[x]; i; i = e[i].nxt) {
      int y = e[i].to;
      if (f[y][S] > f[x][S] + a[y]) {
        f[y][S] = f[x][S] + a[y];
        pre[y][S] = make_pair(x, S);
        if (!vis[y]) {
          vis[y] = 1;
          q.push(y);
        }
      }
    }
  }
}
void Print(int x, int y) {
  ans[x] = 1;
  if (!pre[x][y].second) {
    return;
  }
  Print(pre[x][y].first, pre[x][y].second);
  if (pre[x][y].second != y) {
    Print(x, y ^ pre[x][y].second);
  }
}
signed main() {
  ios::sync_with_stdio(0);
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[(i - 1) * m + j];
      if (i != 1) {
        Build((i - 1) * m + j, (i - 2) * m + j);
      }
      if (j != 1) {
        Build((i - 1) * m + j, (i - 1) * m + j - 1);
      }
      if (i != n) {
        Build((i - 1) * m + j, i * m + j);
      }
      if (j != m) {
        Build((i - 1) * m + j, (i - 1) * m + j + 1);
      }
    }
  }
  int len = (1 << k) - 1;
  for (int S = 0; S <= len; S++) {
    for (int i = 1; i <= n * m; i++) {
      f[i][S] = inf;
    }
  }
  for (int i = 1; i <= k; i++) {
    int x, y;
    cin >> x >> y;
    f[(x - 1) * m + y][1 << (i - 1)] = a[(x - 1) * m + y];
    ans[(x - 1) * m + y] = 1;
  }
  for (int S = 1; S <= len; S++) {
    for (int Sx = (S - 1) & S; Sx; Sx = (Sx - 1) & S) {
      for (int i = 1; i <= n * m; i++) {
        if (f[i][Sx] + f[i][S ^ Sx] - a[i] < f[i][S]) {
          f[i][S] = f[i][Sx] + f[i][S ^ Sx] - a[i];
          pre[i][S] = make_pair(i, Sx);
        }
      }
    }
    for (int i = 1; i <= n * m; i++) {
      if (f[i][S] != inf) {
        vis[i] = 1;
        q.push(i);
      }
    }
    bfs(S);
  }
  int Ans = inf, p = 0;
  for (int i = 1; i <= n * m; i++) {
    if (Ans > f[i][len]) {
      p = i;
      Ans = f[i][len];
    }
  }
  cout << Ans << endl;
  Print(p, len);
  for (int i = 1; i <= n * m; i++) {
    cout << (ans[i] ? 'X' : '.');
    if (i % m == 0) {
      cout << endl;
    }
  }
  return 0;
}
