#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int M = 1505;

int n, l[M][M], r[M][M], gr[M][M];
bool reach[M][M];
char g[M][M];
ll ans;

void go(int i, int k) {
  int s = 0;
  int left = 0, right = -1;
  for (int j = 0; j < n; ++j) {
  // cout << l[k][j] << ' ' << r[k][j] << endl;
    if (r[k][j] == -1)
      continue;
    while (right < r[k][j]) {
      right++;
      if (reach[k][right])
        s += g[k][right];
    } while (left < l[k][j]) {
      if (reach[k][left])
        s -= g[k][left];
      left++;
    }
    ans += g[i][j]*s;
  }
  // cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) {
      cin >> g[i][j];
      if (g[i][j] != '#')
        g[i][j] -= '0';
    }

  for (int i = 0; i < n; ++i)
    for (int j = n-1; j >= 0; --j) {
      if (g[i][j] == '#')
        gr[i][j] = -1;
      else
        gr[i][j] = max(j, gr[i][j+1]);
    }

  for (int i = 0; i < n; ++i) {
    memset(l, 0, sizeof(l));
    memset(r, 0, sizeof(r));
    memset(reach, 0, sizeof(reach));
    for (int j = 0; j < n; ++j) {
      if (g[i][j] == '#') {
        r[i][j] = -1;
        l[i][j] = n;
        reach[i][j] = 0;
      } else {
        l[i][j] = j;
        r[i][j] = gr[i][j];
        reach[i][j] = 1;
      }
    }
    go(i, i);
    for (int k = i; k < n-1; ++k) {
      //update row k to row k+1
      for (int j = 0; j < n; ++j)
        if (g[k+1][j] != '#')
          reach[k+1][j] = reach[k][j];
      for (int j = 0; j < n-1; ++j)
        if (g[k+1][j+1] != '#')
          reach[k+1][j+1] |= reach[k+1][j];
      int right = -1, left = 0, best = -1, sz = 0;
      for (int j = 0; j < n; ++j) {
        if (r[k][j] == -1) {
          r[k+1][j] = -1;
          continue;
        }
        while (right < r[k][j]) {
          right++;
          if (!reach[k][right])
            continue;
          if (g[k+1][right] != '#') {
            best = gr[k+1][right];
            sz++;
          }
        } while (left < l[k][j]) {
          if (!reach[k][left]) {
            left++;
            continue;
          }
          if (g[k+1][left] != '#')
            sz--;
          left++;
        }
        if (sz == 0)
          r[k+1][j] = -1;
        else
          r[k+1][j] = best;
      }
      right = n-1, left = n, best = n, sz = 0;
      for (int j = n-1; j >= 0; --j) {
        if (r[k][j] == -1) {
          l[k+1][j] = n;
          continue;
        }
        while (left > l[k][j]) {
          left--;
          if (!reach[k][left])
            continue;
          if (g[k+1][left] != '#') {
            best = left;
            sz++;
          }
        } while (right > r[k][j]) {
          if (!reach[k][right]) {
            right--;
            continue;
          }
          if (g[k+1][right] != '#')
            sz--;
          right--;
        }
        if (sz == 0)
          l[k+1][j] = n;
        else
          l[k+1][j] = best;
      }
      go(i, k+1);
    }
  }

  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      if (g[i][j] != '#')
        ans -= g[i][j]*g[i][j];
  cout << ans << endl;
}

Measured memory_bytes: 25856