#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int DISJOINTED = 1;
const int TRASITIVE = 2;
int r, c, k;
int bd[100][100];
int xs[7];
int ys[7];
int dp[100][100][1 << 7];
int from[100][100][1 << 7][3];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
bool pave[100][100];
void update(int mask) {
  for (int k = 0; k < (int)(r * c + 10); k++) {
    for (int i = 0; i < (int)(r); i++)
      for (int j = 0; j < (int)(c); j++) {
        for (int d = 0; d < (int)(4); d++) {
          int x = i + dx[d];
          int y = j + dy[d];
          if (0 <= x && x < r && 0 <= y && y < c) {
            int tmp = dp[i][j][mask] + bd[x][y];
            if (tmp < dp[x][y][mask]) {
              dp[x][y][mask] = tmp;
              from[x][y][mask][0] = TRASITIVE;
              from[x][y][mask][1] = i;
              from[x][y][mask][2] = j;
            }
          }
        }
      }
  }
}
void fillSquare(int x, int y, int mask) {
  pave[x][y] = true;
  if (from[x][y][mask][0] == TRASITIVE)
    fillSquare(from[x][y][mask][1], from[x][y][mask][2], mask);
  else if (from[x][y][mask][0] == DISJOINTED) {
    fillSquare(x, y, from[x][y][mask][1]);
    fillSquare(x, y, from[x][y][mask][2]);
  }
}
int main() {
  cin >> r >> c >> k;
  for (int i = 0; i < (int)(r); i++)
    for (int j = 0; j < (int)(c); j++) scanf("%d", bd[i] + j);
  for (int i = 0; i < (int)(k); i++) {
    scanf("%d %d", xs + i, ys + i);
    --xs[i];
    --ys[i];
  }
  for (int i = 0; i < (int)(r); i++)
    for (int j = 0; j < (int)(c); j++)
      for (int mask = 0; mask < (int)(1 << k); mask++) {
        dp[i][j][mask] = 999999999;
        from[i][j][mask][0] = 0;
      }
  for (int i = 0; i < (int)(k); i++)
    dp[xs[i]][ys[i]][1 << i] = bd[xs[i]][ys[i]];
  for (int mask = 0; mask < (int)(1 << k); mask++) {
    for (int i = 0; i < (int)(r); i++)
      for (int j = 0; j < (int)(c); j++) {
        for (int sub = (mask - 1) & mask; sub > 0; sub = (sub - 1) & mask) {
          int tmp = dp[i][j][sub] + dp[i][j][mask ^ sub] - bd[i][j];
          if (tmp < dp[i][j][mask]) {
            dp[i][j][mask] = tmp;
            from[i][j][mask][0] = DISJOINTED;
            from[i][j][mask][1] = sub;
            from[i][j][mask][2] = mask ^ sub;
          }
        }
      }
    update(mask);
  }
  int ret = 999999999;
  int x;
  int y;
  for (int i = 0; i < (int)(r); i++)
    for (int j = 0; j < (int)(c); j++) {
      if (dp[i][j][(1 << k) - 1] < ret) {
        ret = dp[i][j][(1 << k) - 1];
        x = i;
        y = j;
      }
    }
  cout << ret << endl;
  memset(pave, 0, sizeof(pave));
  fillSquare(x, y, (1 << k) - 1);
  for (int i = 0; i < (int)(r); i++)
    for (int j = 0; j < (int)(c); j++) {
      if (pave[i][j])
        cout << 'X';
      else
        cout << '.';
      if (j == c - 1) cout << endl;
    }
  return 0;
}
