#include <bits/stdc++.h>
using namespace std;
int ans = 2e9, cnt, n, m, k, X, Y, a[110][110], Ans[110][110],
    dis[110][110][130], path[110][110][130][3];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
bool vis[110][110][130];
struct point {
  int x, y, mask;
};
queue<point> q;
void draw(int x, int y, int mask) {
  Ans[x][y] = 1;
  if (path[x][y][mask][0] == 1) {
    draw(path[x][y][mask][1], path[x][y][mask][2], mask);
  }
  if (path[x][y][mask][0] == 2) {
    draw(x, y, path[x][y][mask][1]);
    draw(x, y, path[x][y][mask][2]);
  }
}
void SPFA() {
  while (!q.empty()) {
    point u = q.front();
    q.pop();
    vis[u.x][u.y][u.mask] = 0;
    for (int i = 0; i < 4; i++) {
      point v = (point){u.x + dx[i], u.y + dy[i], u.mask};
      if (v.x < 1 || v.x > n || v.y < 1 || v.y > m) continue;
      if (dis[v.x][v.y][v.mask] > dis[u.x][u.y][u.mask] + a[v.x][v.y]) {
        dis[v.x][v.y][v.mask] = dis[u.x][u.y][u.mask] + a[v.x][v.y];
        path[v.x][v.y][v.mask][0] = 1, path[v.x][v.y][v.mask][1] = u.x,
        path[v.x][v.y][v.mask][2] = u.y;
        if (!vis[v.x][v.y][v.mask]) vis[v.x][v.y][v.mask] = 1, q.push(v);
      }
    }
    for (int i = 0; i < (1 << k); i++) {
      if (i & u.mask) continue;
      point v = (point){u.x, u.y, u.mask | i};
      if (dis[v.x][v.y][v.mask] >
          dis[u.x][u.y][u.mask] + dis[u.x][u.y][i] - a[u.x][u.y]) {
        dis[v.x][v.y][v.mask] =
            dis[u.x][u.y][u.mask] + dis[u.x][u.y][i] - a[u.x][u.y];
        path[v.x][v.y][v.mask][0] = 2, path[v.x][v.y][v.mask][1] = i,
        path[v.x][v.y][v.mask][2] = u.mask;
        if (!vis[v.x][v.y][v.mask]) vis[v.x][v.y][v.mask] = 1, q.push(v);
      }
    }
  }
}
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
    }
  }
  memset(dis, 0x7f, sizeof(dis));
  for (int i = 1, x, y; i <= k; i++) {
    cin >> x >> y;
    vis[x][y][1 << (i - 1)] = 1;
    dis[x][y][1 << (i - 1)] = a[x][y];
    q.push((point){x, y, 1 << (i - 1)});
  }
  SPFA();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (dis[i][j][(1 << k) - 1] < ans) {
        ans = dis[i][j][(1 << k) - 1];
        X = i;
        Y = j;
      }
    }
  }
  draw(X, Y, (1 << k) - 1);
  cout << ans << endl;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      putchar(Ans[i][j] ? 'X' : '.');
    }
    putchar('\n');
  }
  return 0;
}
