#include <bits/stdc++.h>
using namespace std;
const int INFTY = 1 << 29;
void build(vector<string>& ans, int bit, int i, vector<vector<int> >& flg,
           vector<vector<int> >& path) {
  int h = ans.size(), w = ans[0].size();
  ans[i / w][i % w] = 'X';
  if (flg[bit][i] == 1) {
    build(ans, path[bit][i], i, flg, path);
    build(ans, bit ^ path[bit][i], i, flg, path);
  }
  if (flg[bit][i] == 2) {
    build(ans, bit, path[bit][i], flg, path);
  }
}
int main() {
  for (int h, w, c; cin >> h >> w >> c && h | w | c;) {
    vector<vector<int> > grid(h, vector<int>(w));
    vector<int> ps(c);
    for (int i = int(0); i < int(h); i++)
      for (int j = int(0); j < int(w); j++) cin >> grid[i][j];
    for (int i = int(0); i < int(c); i++) {
      int a, b;
      cin >> a >> b;
      ps[i] = (a - 1) * w + (b - 1);
    }
    int n = h * w;
    vector<vector<int> > dist(n, vector<int>(n, INFTY));
    vector<vector<int> > prev(n, vector<int>(n, -1));
    for (int i = int(0); i < int(h); i++)
      for (int j = int(0); j < int(w); j++) {
        dist[i * w + j][i * w + j] = grid[i][j];
        int di[] = {-1, 1, 0, 0}, dj[] = {0, 0, -1, 1};
        for (int k = int(0); k < int(4); k++) {
          int ii = i + di[k], jj = j + dj[k];
          if (0 <= ii && ii < h && 0 <= jj && jj < w) {
            dist[i * w + j][ii * w + jj] = grid[i][j] + grid[ii][jj];
            prev[i * w + j][ii * w + jj] = i * w + j;
          }
        }
      }
    for (int k = int(0); k < int(n); k++)
      for (int i = int(0); i < int(n); i++)
        for (int j = int(0); j < int(n); j++) {
          int temp = dist[i][k] + dist[k][j] - dist[k][k];
          if (dist[i][j] > temp) {
            dist[i][j] = temp;
            prev[i][j] = prev[k][j];
          }
        }
    vector<vector<int> > dp(1 << c, vector<int>(n, INFTY));
    vector<vector<int> > flg(1 << c, vector<int>(n, 0));
    vector<vector<int> > path(1 << c, vector<int>(n, 0));
    for (int i = int(0); i < int(c); i++)
      for (int j = int(0); j < int(n); j++) {
        dp[1 << i][j] = dist[ps[i]][j];
        if (j != ps[i]) {
          flg[1 << i][j] = 2;
          path[1 << i][j] = prev[ps[i]][j];
        }
      }
    for (int bit = int(0); bit < int(1 << c); bit++) {
      for (int i = int(0); i < int(n); i++)
        for (int j = int(0); j < int(n); j++) {
          for (int mask = bit; mask; mask = (mask - 1) & bit) {
            int temp =
                dp[mask][j] + dp[bit ^ mask][j] + dist[j][i] - dist[j][j] * 2;
            if (dp[bit][i] > temp) {
              dp[bit][i] = temp;
              if (j == i) {
                flg[bit][i] = 1;
                path[bit][i] = mask;
              } else {
                flg[bit][i] = 2;
                path[bit][i] = prev[j][i];
              }
            }
          }
        }
    }
    int res = INFTY;
    for (int i = int(0); i < int(n); i++) res = min(res, dp[(1 << c) - 1][i]);
    vector<string> ans(h, string(w, '.'));
    for (int i = int(0); i < int(n); i++)
      if (dp[(1 << c) - 1][i] == res) {
        build(ans, (1 << c) - 1, i, flg, path);
        break;
      }
    cout << res << endl;
    for (int i = int(0); i < int(h); i++) cout << ans[i] << endl;
  }
  return 0;
}
