#include <bits/stdc++.h>
using namespace std;
int garden[100][100], n, m, k;
int dist[200][200], mid[200][200], f[1 << 6][200];
bool road[100][100];
struct Building {
  int x, y, pos;
} build[7];
struct Path {
  int mask1, mask2, pos;
  void assign(int mask1, int mask2, int pos) {
    this->mask1 = mask1;
    this->mask2 = mask2;
    this->pos = pos;
  }
} path[1 << 6][200];
void output() {
  int cost = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (road[i][j]) {
        cost += garden[i][j];
      }
    }
  }
  cout << cost << endl;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cout << (road[i][j] ? 'X' : '.');
    }
    cout << endl;
  }
}
void Floyd() {
  int dir[4][2] = {0, -1, 0, 1, 1, 0, -1, 0}, nm = n * m;
  for (int i = 0; i < nm; ++i) {
    for (int j = 0; j < nm; ++j) {
      dist[i][j] = (i == j ? 0 : 1000000000);
      mid[i][j] = -1;
    }
  }
  for (int i = 0; i < nm; ++i) {
    int x = i / m, y = i % m;
    for (int k = 0; k < 4; ++k) {
      int rx = x + dir[k][0], ry = y + dir[k][1];
      if (0 <= rx && rx < n && 0 <= ry && ry < m) {
        dist[i][rx * m + ry] = garden[x][y];
      }
    }
  }
  for (int k = 0; k < nm; ++k) {
    for (int i = 0; i < nm; ++i) {
      for (int j = 0; j < nm; ++j) {
        if (dist[i][k] + dist[k][j] < dist[i][j]) {
          dist[i][j] = dist[i][k] + dist[k][j];
          mid[i][j] = k;
        }
      }
    }
  }
}
void buildroad0(int x, int y) {
  if (mid[x][y] == -1) {
    road[x / m][x % m] = true;
  } else {
    buildroad0(x, mid[x][y]);
    buildroad0(mid[x][y], y);
  }
}
void buildroad(int mask, int pos) {
  if (mask) {
    buildroad(path[mask][pos].mask1, path[mask][pos].pos);
    buildroad(path[mask][pos].mask2, path[mask][pos].pos);
    buildroad0(path[mask][pos].pos, pos);
  }
}
int main() {
  while (cin >> n >> m >> k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        cin >> garden[i][j];
      }
    }
    for (int i = 0; i < k; ++i) {
      cin >> build[i].x >> build[i].y;
      --build[i].x, --build[i].y;
      build[i].pos = build[i].x * m + build[i].y;
    }
    if (k == 1) {
      road[build[0].x][build[0].y] = true;
      output();
      continue;
    }
    Floyd();
    memset(f, 0x0f, sizeof(f));
    int nm = n * m, k2 = 1 << (k - 1);
    for (int i = 0; i < k - 1; ++i) {
      for (int j = 0; j < nm; ++j) {
        f[1 << i][j] = dist[build[i].pos][j];
        path[1 << i][j].assign(0, 0, build[i].pos);
      }
    }
    for (int mask1 = 1; mask1 < k2; ++mask1) {
      for (int mask2 = 1; mask2 < k2; ++mask2) {
        if ((mask1 & mask2) == 0) {
          for (int pos1 = 0; pos1 < nm; ++pos1) {
            for (int pos2 = 0; pos2 < nm; ++pos2) {
              int d = dist[pos1][pos2] + f[mask1][pos1] + f[mask2][pos1];
              if (d < f[mask1 | mask2][pos2]) {
                f[mask1 | mask2][pos2] = d;
                path[mask1 | mask2][pos2].assign(mask1, mask2, pos1);
              }
            }
          }
        }
      }
    }
    buildroad(k2 - 1, build[k - 1].pos);
    road[build[k - 1].x][build[k - 1].y] = true;
    output();
  }
}
