#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
inline int read() {
  int X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
struct node {
  int msk, x, y;
  node() {}
  node(int _mask, int _x, int _y) {
    msk = _mask;
    x = _x;
    y = _y;
  }
};
node pre[505][105][105];
queue<node> q;
int n, m, k;
int dat[105][105];
int dp[505][105][105];
bool use[105][105];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
bool hav[505][105][105];
void printmsk(int msk) {
  string s;
  while (msk) {
    s += char(msk & 1);
    msk >>= 1;
  }
  return reverse(s.begin(), s.end());
}
void solve(int msk, int x, int y) {
  if (pre[msk][x][y].msk == -1) {
    return;
  }
  if (pre[msk][x][y].msk == 0) {
    solve(pre[msk][x][y].x, x, y);
    solve(pre[msk][x][y].y, x, y);
  } else {
    use[x][y] = true;
    solve(msk, pre[msk][x][y].x, pre[msk][x][y].y);
  }
}
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> dat[i][j];
    }
  }
  for (int msk = 0; msk < (1 << k); msk++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        dp[msk][i][j] = INF;
        pre[msk][i][j] = node(-1, -1, -1);
      }
    }
  }
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    dp[(1 << i)][x][y] = dat[x][y];
    use[x][y] = true;
    q.push(node(1 << i, x, y));
    hav[(1 << i)][x][y] = true;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (!use[i][j]) {
        dp[0][i][j] = dat[i][j];
        q.push(node(0, i, j));
        hav[0][i][j] = true;
      }
    }
  }
  while (q.size()) {
    node x = q.front();
    q.pop();
    hav[x.msk][x.x][x.y] = false;
    for (int msk = 1; msk < (1 << k); msk++) {
      if ((msk & x.msk) == 0) {
        if (dp[msk ^ x.msk][x.x][x.y] >
            dp[x.msk][x.x][x.y] + dp[msk][x.x][x.y] - dat[x.x][x.y]) {
          pre[msk ^ x.msk][x.x][x.y] = node(0, x.msk, msk);
          dp[msk ^ x.msk][x.x][x.y] =
              dp[x.msk][x.x][x.y] + dp[msk][x.x][x.y] - dat[x.x][x.y];
          if (!hav[msk ^ x.msk][x.x][x.y]) {
            hav[msk ^ x.msk][x.x][x.y] = true;
            q.push(node(msk ^ x.msk, x.x, x.y));
          }
        }
      }
    }
    for (int i = 0; i < 4; i++) {
      int nx = x.x + dx[i];
      int ny = x.y + dy[i];
      if (nx >= 1 and nx <= n and ny >= 1 and ny <= m) {
        if (dp[x.msk][nx][ny] > dp[x.msk][x.x][x.y] + dat[nx][ny]) {
          pre[x.msk][nx][ny] = node(1, x.x, x.y);
          dp[x.msk][nx][ny] = dp[x.msk][x.x][x.y] + dat[nx][ny];
          hav[x.msk][nx][ny] = true;
          q.push(node(x.msk, nx, ny));
        }
      }
    }
  }
  int ans = INF;
  node tmp;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (dp[(1 << k) - 1][i][j] < ans) {
        ans = dp[(1 << k) - 1][i][j];
        tmp = node((1 << k) - 1, i, j);
      }
    }
  }
  cout << ans << endl;
  solve(tmp.msk, tmp.x, tmp.y);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (use[i][j]) {
        cout << "X";
      } else {
        cout << ".";
      }
    }
    cout << endl;
  }
  return 0;
}
