#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int n, m, k;
int a[220];
int dis[220][220];
int dp[220][220];
string s[220];
void floyd() {
  memset(dis, 0x3f, sizeof(dis));
  for (int x = 0; x < n * m; x++) dis[x][x] = a[x];
  for (int x = 0; x < n; x++)
    for (int y = 0; y < m; y++) {
      for (int d = 0; d < 4; d++) {
        int xx = x + dx[d], yy = y + dy[d];
        if (xx < 0 || yy < 0 || xx >= n || yy >= m) continue;
        dis[x * m + y][xx * m + yy] = a[x * m + y] + a[xx * m + yy];
        dis[xx * m + yy][x * m + y] = a[x * m + y] + a[xx * m + yy];
      }
    }
  for (int i = 0; i < n * m; i++)
    for (int j = 0; j < n * m; j++)
      if (i != j && dis[i][j] < 0x3f3f3f3f)
        for (int x = 0; x < n * m; x++)
          if (x != j && x != i && dis[x][i] < 0x3f3f3f3f) {
            dis[x][j] = min(dis[x][j], dis[i][j] + dis[x][i] - a[i]);
            dis[j][x] = min(dis[j][x], dis[i][j] + dis[x][i] - a[i]);
          }
}
vector<int> po;
void cover2(int aa, int b) {
  s[aa / m][aa % m] = 'X';
  s[b / m][b % m] = 'X';
  for (int c = 0; c < n * m; c++)
    if (aa != c && b != c && dis[aa][c] + dis[c][b] - a[c] == dis[aa][b]) {
      cover2(aa, c);
      cover2(c, b);
      return;
    }
}
void cover(int msk, int pos, int val) {
  s[pos / m][pos % m] = 'X';
  for (int xx = msk; xx; xx = (xx - 1) & msk) {
    if (xx == msk) continue;
    if (dp[pos][xx] + dp[pos][msk ^ xx] - a[pos] == val) {
      cover(xx, pos, dp[pos][xx]);
      cover(msk ^ xx, pos, dp[pos][msk ^ xx]);
      return;
    }
  }
  for (int ppos = 0; ppos < n * m; ppos++) {
    if (ppos == pos) continue;
    if (dp[ppos][msk] + dis[pos][ppos] - a[ppos] == val) {
      cover2(pos, ppos);
      cover(msk, ppos, dp[ppos][msk]);
      return;
    }
  }
  int cnt = 0;
  for (int i = 0; i < k; i++)
    if (msk >> i & 1) cnt++;
  if (cnt == 1) {
    for (int i = 0; i < k; i++)
      if (msk >> i & 1) cover2(pos, po[i]);
  }
}
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    s[i].resize(m);
    for (int j = 0; j < m; j++) s[i][j] = '.';
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> a[i * m + j];
  floyd();
  po.resize(k);
  memset(dp, 0x3f, sizeof(dp));
  for (int i = 0; i < k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--, y--;
    s[x][y] = 'X';
    po[i] = (x * m + y);
    for (int pos = 0; pos < n * m; pos++) dp[pos][1 << i] = dis[po[i]][pos];
  }
  for (int i = 0; i < (1 << k); i++) {
    for (int pos = 0; pos < n * m; pos++)
      for (int ppos = 0; ppos < n * m; ppos++)
        dp[ppos][i] = min(dp[ppos][i], dp[pos][i] + dis[pos][ppos] - a[pos]);
    for (int pos = 0; pos < n * m; pos++)
      if (dp[pos][i] < 0x3f3f3f3f) {
        for (int ii = 0; ii < (1 << k); ii++) {
          dp[pos][i | ii] =
              min(dp[pos][i | ii], dp[pos][i] + dp[pos][ii] - a[pos]);
        }
      }
  }
  int ans = 0x3f3f3f3f, ipos;
  for (int i = 0; i < n * m; i++)
    if (dp[i][(1 << k) - 1] < ans) {
      ans = dp[i][(1 << k) - 1];
      ipos = i;
    }
  cover((1 << k) - 1, ipos, ans);
  cout << ans << endl;
  for (int i = 0; i < n; i++) {
    cout << s[i] << endl;
  }
  return 0;
}
