#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const long long BIG = 1446803456761533460LL;
const int Big = 336860180;
stringstream sss;
const int maxn = 210;
const int maxk = 7;
const int maxk2 = 1 << maxk;
int n, w, h, k;
int A[maxn];
int G[maxk];
int d[maxn][maxn];
bitset<maxn> di[maxn][maxn];
int dp[maxk2][maxn];
bitset<maxn> dpi[maxk2][maxn];
void MAIN() {
  cin >> h >> w >> k;
  for (int i = (0); i < (h); ++i) {
    for (int j = (0); j < (w); ++j) {
      cin >> A[i * w + j];
    }
  }
  for (int i = (0); i < (k); ++i) {
    int a, b;
    cin >> a >> b;
    G[i] = (a - 1) * w + (b - 1);
  }
  n = h * w;
  memset(d, 20, sizeof d);
  for (int i = (0); i < (h); ++i) {
    for (int j = (0); j < (w); ++j) {
      int x = i * w + j;
      if (i > 0) {
        d[x][x - w] = A[x] + A[x - w];
        di[x][x - w][x] = di[x][x - w][x - w] = 1;
      }
      if (j > 0) {
        d[x][x - 1] = A[x] + A[x - 1];
        di[x][x - 1][x] = di[x][x - 1][x - 1] = 1;
      }
      if (i + 1 < h) {
        d[x][x + w] = A[x] + A[x + w];
        di[x][x + w][x] = di[x][x + w][x + w] = 1;
      }
      if (j + 1 < w) {
        d[x][x + 1] = A[x] + A[x + 1];
        di[x][x + 1][x] = di[x][x + 1][x + 1] = 1;
      }
    }
  }
  for (int i = (0); i < (n); ++i) {
    d[i][i] = A[i];
    di[i][i][i] = 1;
  }
  for (int k = (0); k < (n); ++k) {
    for (int i = (0); i < (n); ++i) {
      for (int j = (0); j < (n); ++j) {
        if (d[i][k] + d[k][j] - A[k] < d[i][j]) {
          d[i][j] = d[i][k] + d[k][j] - A[k];
          di[i][j] = di[i][k] | di[k][j];
        }
      }
    }
  }
  memset(dp, 20, sizeof dp);
  for (int i = (0); i < (k); ++i) {
    for (int j = (0); j < (n); ++j) {
      dp[1 << i][j] = d[j][G[i]];
      dpi[1 << i][j] = di[j][G[i]];
    }
  }
  int k2 = 1 << k;
  for (int i = (1); i < (k2); ++i) {
    for (int j = (0); j < (n); ++j) {
      int mn = Big;
      pair<int, int> mni;
      for (int sub = (i - 1) & i; sub > 0; sub = (sub - 1) & i) {
        for (int v = (0); v < (n); ++v) {
          int val = dp[sub][v] + dp[i ^ sub][v] + d[j][v] - A[v] * 2;
          if (val < mn) {
            mn = val;
            mni = {sub, v};
          }
        }
      }
      if (mn < dp[i][j]) {
        dp[i][j] = mn;
        dpi[i][j] = dpi[mni.first][mni.second] |
                    dpi[i ^ mni.first][mni.second] | di[j][mni.second];
      }
    }
  }
  int mni = 0;
  for (int i = (0); i < (n); ++i) {
    if (dp[k2 - 1][i] < dp[k2 - 1][mni]) {
      mni = i;
    }
  }
  cout << dp[k2 - 1][mni] << '\n';
  for (int i = (0); i < (h); ++i) {
    for (int j = (0); j < (w); ++j) {
      cout << (dpi[k2 - 1][mni][i * w + j] ? 'X' : '.');
    }
    cout << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  sss << R"(
4 5 4
1 4 5 1 2
2 2 2 2 7
2 4 1 4 5
3 2 1 7 1
1 1
1 5
4 1
4 4
    )";
  MAIN();
  return 0;
}
