#include<bits/stdc++.h>

using namespace std;

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, ans = 2 * 1e9 + 1e5;
  cin >> n;
  vector<string> vs(n);
  for (int i = 0; i < n; i++)
    cin >> vs[i];
  vector<vector<int>> v(n, vector<int>(n)), dp(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> v[i][j];
  }
  dp[n - 1][n - 1] = v[n - 1][n - 1];
  for (int i = n - 2; i >= 0; i--)
    dp[i][n - 1] = v[i][n - 1] + dp[i + 1][n - 1];
  for (int i = n - 2; i >= 0; i--)
    dp[n - 1][i] = v[n - 1][i] + dp[n - 1][i + 1];
  for (int i = n - 2; i >= 0; i--) {
    for (int j = n - 2; j >= 0; j--)
      dp[i][j] = v[i][j] + min(dp[i][j + 1], dp[i + 1][j]);
  }
  if (vs[0][0] == '0') {
    cout << dp[0][0];
    return 0;
  }
  vector<vector<int>> vv(n, vector<int>(n));
  vector<vector<int>> vp(n, vector<int>(n));
  vv[0][0] = 1;
  vector<pair<int, int>> vpo, vpc, vvo, vvc;
  vvo.push_back({0, 0});
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (vs[i][j] == '0') {
        vpo.push_back({i, j});
        vp[i][j] = true;
      }
    }
  }
  while (vvo.size() != 0) {
    for (pair<int, int> p : vpo) {
      int i = p.first, j = p.second;
      if (i != n - 1 and vp[i + 1][j] == 0) {
        vpc.push_back({i + 1, j});
        vp[i + 1][j] = 1;
      }
      if (j != n - 1 and vp[i][j + 1] == 0) {
        vpc.push_back({i, j + 1});
        vp[i][j + 1] = 1;
      }
      if (i != 0 and vp[i - 1][j] == 0) {
        vpc.push_back({i - 1, j});
        vp[i - 1][j] = 1;
      }
      if (j != 0 and vp[i][j - 1] == 0) {
        vpc.push_back({i, j - 1});
        vp[i][j - 1] = 1;
      }
    }
    vpo = vpc;
    vpc.clear();
    /*for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        cout << vp[i][j] << " ";
      cout << "\n";
    }
    cout << "\n";*/
    for (pair<int, int> p : vvo) {
      int i = p.first, j = p.second;
      if (i != n - 1 and vv[i + 1][j] == 0 and vp[i + 1][j] == 0) {
        vvc.push_back({i + 1, j});
        vv[i + 1][j] = 1;
      }
      if (j != n - 1 and vv[i][j + 1] == 0 and vp[i][j + 1] == 0) {
        vvc.push_back({i, j + 1});
        vv[i][j + 1] = 1;
      }
    }
    vvo = vvc;
    vvc.clear();
  }
  if (vv[n - 1][n - 1])
    ans = 0;
  vector<pair<int, int>> vvn;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (vv[i][j] == 1)
        continue;
      if (i != 0 and vv[i - 1][j] == 1)
        vvn.push_back({i, j});
      if (j != 0 and vv[i][j - 1] == 1)
        vvn.push_back({i, j});
    }
  }
  for (pair<int, int> p : vvn)
    vv[p.first][p.second] = 1;
  vv[0][0] = 0;
  /*for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cout << vv[i][j] << " ";
    cout << "\n";
  }
  cout << "\n";*/
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (vv[i][j] == 1)
        ans = min(dp[i][j], ans);
    }
  }
  cout << ans;
  return 0;
}