#include <bits/stdc++.h>
using namespace std;
const int wayx[4] = {0, 0, -1, 1};
const int wayy[4] = {1, -1, 0, 0};
int n, m, k;
int a[110][110];
int c[110][110];
int f[110][110][1 << 7];
int pp[110][110][1 << 7];
int b[110][110];
const int inf = 1e9;
struct node {
  int x, y, s, z;
  friend bool operator<(node a, node b) { return a.z > b.z; }
};
vector<node> way[110][110][1 << 7];
priority_queue<node> q;
queue<node> tmp;
int main() {
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      for (int l = 0; l < (1 << k); l++) f[i][j][l] = inf;
  for (int i = 1; i <= k; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    c[x][y] = 1 << (i - 1);
    f[x][y][c[x][y]] = a[x][y];
    q.push({x, y, c[x][y], a[x][y]});
  }
  while (!q.empty()) {
    node p = q.top();
    q.pop();
    if (pp[p.x][p.y][p.s]) continue;
    pp[p.x][p.y][p.s] = 1;
    for (int i = 0; i < 4; i++) {
      int nx = p.x + wayx[i], ny = p.y + wayy[i], ns;
      if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
      ns = p.s | c[nx][ny];
      if (f[nx][ny][ns] > p.z + a[nx][ny]) {
        f[nx][ny][ns] = p.z + a[nx][ny];
        way[nx][ny][ns].clear();
        way[nx][ny][ns].push_back({p.x, p.y, p.s});
        q.push({nx, ny, ns, f[nx][ny][ns]});
      }
    }
    int cf = (~p.s) & ((1 << k) - 1);
    for (int i = cf; i; i = (i - 1) & cf) {
      int nx = p.x, ny = p.y, ns = p.s | i;
      if (f[nx][ny][i] == inf) continue;
      if (f[nx][ny][ns] > p.z + f[nx][ny][i] - a[nx][ny]) {
        f[nx][ny][ns] = p.z + f[nx][ny][i] - a[nx][ny];
        way[nx][ny][ns].clear();
        way[nx][ny][ns].push_back({nx, ny, p.s});
        way[nx][ny][ns].push_back({nx, ny, i});
        q.push({nx, ny, ns, f[nx][ny][ns]});
      }
    }
  }
  int ans = inf;
  node p;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (ans > f[i][j][(1 << k) - 1]) {
        ans = min(ans, f[i][j][(1 << k) - 1]);
        p = {i, j, (1 << k) - 1};
      }
    }
  printf("%d\n", ans);
  tmp.push(p);
  while (!tmp.empty()) {
    node p = tmp.front();
    tmp.pop();
    b[p.x][p.y] = 1;
    for (int i = 0; i < way[p.x][p.y][p.s].size(); i++)
      tmp.push(way[p.x][p.y][p.s][i]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      if (b[i][j])
        printf("X");
      else
        printf(".");
    printf("\n");
  }
  return 0;
}
