#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int n, m, k;
int dp[110][110][1024], vis[110][110][1024], a[110][110], cover[110][110];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
struct NODE {
  int x, y, state;
  NODE() {}
  NODE(int _x, int _y, int _state) { x = _x, y = _y, state = _state; }
};
NODE pre[110][110][1024];
void Cover(int x, int y, int state) {
  if (state == -1) return;
  NODE temp = pre[x][y][state];
  if (temp.state == 0) {
    Cover(x, y, temp.x);
    Cover(x, y, temp.y);
  }
  if (temp.state == 1) {
    Cover(temp.x, temp.y, state);
    cover[x][y] = 1;
  }
}
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]);
    }
  }
  queue<NODE> Q;
  memset(dp, INF, sizeof(dp));
  for (int i = 1; i <= k; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    cover[x][y] = 1;
    dp[x][y][1 << (i - 1)] = a[x][y];
    vis[x][y][1 << (i - 1)] = 1;
    Q.push(NODE(x, y, 1 << (i - 1)));
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      pre[i][j][0] = NODE(-1, -1, -1);
      if (!cover[i][j]) {
        dp[i][j][0] = a[i][j];
        Q.push(NODE(i, j, 0));
        vis[i][j][0] = 1;
      }
    }
  }
  while (!Q.empty()) {
    NODE u = Q.front();
    Q.pop();
    vis[u.x][u.y][u.state] = 0;
    for (int i = 1; i < (1 << k); i++) {
      if ((i & u.state) == 0) {
        if (dp[u.x][u.y][u.state ^ i] >
            dp[u.x][u.y][u.state] + dp[u.x][u.y][i] - a[u.x][u.y]) {
          dp[u.x][u.y][u.state ^ i] =
              dp[u.x][u.y][u.state] + dp[u.x][u.y][i] - a[u.x][u.y];
          pre[u.x][u.y][u.state ^ i] = NODE(u.state, i, 0);
          if (!vis[u.x][u.y][u.state ^ i]) {
            vis[u.x][u.y][u.state ^ i] = 1;
            Q.push(NODE(u.x, u.y, u.state ^ i));
          }
        }
      }
    }
    for (int i = 0; i < 4; i++) {
      int xx = u.x + dx[i];
      int yy = u.y + dy[i];
      if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
      if (dp[xx][yy][u.state] > dp[u.x][u.y][u.state] + a[xx][yy]) {
        dp[xx][yy][u.state] = dp[u.x][u.y][u.state] + a[xx][yy];
        vis[xx][yy][u.state] = 1;
        Q.push(NODE(xx, yy, u.state));
        pre[xx][yy][u.state] = NODE(u.x, u.y, 1);
      }
    }
  }
  int min_x, min_y, min_w = INF;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (min_w > dp[i][j][(1 << k) - 1]) {
        min_w = dp[i][j][(1 << k) - 1];
        min_x = i, min_y = j;
      }
    }
  }
  Cover(min_x, min_y, (1 << k) - 1);
  printf("%d\n", min_w);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (cover[i][j])
        printf("X");
      else
        printf(".");
    }
    printf("\n");
  }
  return 0;
}
