#include <bits/stdc++.h>

int main()
{
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int n, m;
  std::cin >> n;
  m = n;
  std::vector<std::vector<int>> a(n + 1, std::vector<int>(m + 1));
  int ok = 0;
  for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= m; j++) {
      char x;
      std::cin >> x;
      a[i][j] = x - '0';
      if(a[i][j] == 0) {
        ok = 1;
      }
    }
  }
  std::vector<std::vector<int>> t(n + 1, std::vector<int>(m + 1));
  for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= m; j++) {
      std::cin >> t[i][j];
    }
  }
  std::vector<std::vector<int>> d(n + 1, std::vector<int>(m + 1, n * m + 1));
  std::vector<std::vector<int>> was(n + 1, std::vector<int>(m + 1, 0));
  std::function<int(int, int)> valid = [&] (int x, int y) {
    return x >= 1 && x <= n && y >= 1 && y <= m && was[x][y] == 0;
  };
  std::queue<std::pair<int, int>> q;
  for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= m; j++) {
      if(a[i][j] == 0) {
        q.push({i, j});
        d[i][j] = 0;
      }
    }
  }
  while(!q.empty()) {
    int x = (q.front()).first;
    int y = (q.front()).second;
    q.pop();
    was[x][y] = 1;
    if(valid(x - 1, y)) {
      d[x - 1][y] = d[x][y] + 1;
      q.push({x - 1, y});
    }

    if(valid(x + 1, y)) {
      d[x + 1][y] = d[x][y] + 1;
      q.push({x + 1, y});
    }

    if(valid(x, y - 1)) {
      d[x][y - 1] = d[x][y] + 1;
      q.push({x, y - 1});
    }

    if(valid(x, y + 1)) {
      d[x][y + 1] = d[x][y] + 1;
      q.push({x , y + 1});
    }

  }
  int inf = 1e9;
  std::vector<std::vector<int>> dp(n + 1, std::vector<int>(m + 1, inf));
  if(a[1][1] == 0) {
    dp[1][1] = t[1][1];
  }
  else {
    dp[1][1] = 0;
  }
  for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= m; j++) {
      if(i == 1 && j == 1) {
        continue;
      }
      if(i + j - 1 > d[i][j]) {
        dp[i][j] = std::min(dp[i - 1][j], dp[i][j - 1]) + t[i][j];
      }
      else {
        dp[i][j] = std::min(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  std::vector<std::vector<int>> dp_min(n + 1, std::vector<int>(m + 1, inf));
  dp_min[n][m] = t[n][m];
  for(int i = n; i >= 1; i--) {
    for(int j = m; j >= 1; j--) {
      if(i == n && j == m) {
        continue;
      }
      if(i + 1 <= n) {
        dp_min[i][j] = std::min(dp_min[i][j], dp_min[i + 1][j] + t[i][j]);
      }
      if(j + 1 <= m) {
        dp_min[i][j] = std::min(dp_min[i][j], dp_min[i][j + 1] + t[i][j]);
      }
    }
  }
  int ans = std::min(dp[n][m], dp_min[1][1]);
  for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= m; j++) {
      if(i + j - 1 <= d[i][j]) {
        ans = std::min(ans, dp[i][j] + dp_min[i][j] - t[i][j]);
      }
      else {
        ans = std::min(ans, dp[i][j] + dp_min[i][j] - t[i][j]);
      }
    }
  }
  std::cout << ans;
}