program reachable_cells
  implicit none
  integer :: n, a(501,501) = 0, i, j, k, dp(500,500) = 0, ans = 0
  character(500) :: s
  read(*,*) n
  a(:,n+1) = -1
  a(n+1,:) = -1
  do i = 1, n
    read(*,*) s
    do j = 1, n
      if (s(j:j) == "#") then
        a(i,j) = -1
        cycle
      end if
      a(i,j) = ichar(s(j:j))-48
    end do
  end do
  do k = 0, 2*(n-1)
    do i = n, max(n-k,1), -1
      j = 2*n-i-k
      if (j < 1 .or. n < j) cycle
      if (a(i,j) < 0) cycle
      if (dp(i+1,j) > 0) dp(i,j) = dp(i,j)+dp(i+1,j)
      if (dp(i,j+1) > 0) dp(i,j) = dp(i,j)+dp(i,j+1)
      if (dp(i+1,j) > 0 .and. dp(i,j+1) > 0 .and. dp(i+1,j+1) > 0) dp(i,j) = dp(i,j)-dp(i+1,j+1)
      ans = ans+a(i,j)*dp(i,j)
      dp(i,j) = dp(i,j)+a(i,j)
    end do
  end do
  write(*,'(i0)') ans
end program reachable_cells