#if 1
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>

auto& in = std::cin;
auto& out = std::cout;

char aaa[50];
int32_t a,b,c,d;
#define beet(x,y,z) if(a x b y c z d==7){out<<a<<#x<<b<<#y<<c<<#z<<d<< "=7\n";return 0;}
int main()
{
	using std::endl;
	in.sync_with_stdio(false);
	out.sync_with_stdio(false);
	in.tie(nullptr);
	out.tie(nullptr);

	in >> aaa;
	a = aaa[0] - '0';
	b = aaa[1] - '0';
	c = aaa[2] - '0';
	d = aaa[3] - '0';
	beet(+,+,+);
	beet(+,+,-);
	beet(+,-,+);
	beet(+,-,-);
	beet(-,+,+);
	beet(-,+,-);
	beet(-,-,+);
	beet(-,-,-);
	//if (a x b y c z == 7) { out << a << #x << b << #y << c << #z << "=7\n"; return 0; }
	//if (a x b y c z == 7) { out << a << #x << b << #y << c << #z << "=7\n"; return 0; }
	//if (a x b y c z == 7) { out << a << #x << b << #y << c << #z << "=7\n"; return 0; }
	//if (a x b y c z == 7) { out << a << #x << b << #y << c << #z << "=7\n"; return 0; }
	//if (a x b y c z == 7) { out << a << #x << b << #y << c << #z << "=7\n"; return 0; }
	//if (a x b y c z == 7) { out << a << #x << b << #y << c << #z << "=7\n"; return 0; }
	//if (a x b y c z == 7) { out << a << #x << b << #y << c << #z << "=7\n"; return 0; }
	//if (a x b y c z == 7) { out << a << #x << b << #y << c << #z << "=7\n"; return 0; }

	return 0;
}
#endif


#if 0
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>

auto& in = std::cin;
auto& out = std::cout;


#include <queue>
#include <vector>
#include <functional>
#include <utility>
#include <algorithm>
#include <iterator>

using COST_T = uint32_t;
constexpr uint32_t N_MAX = 11;
constexpr COST_T INF = 100000000;
struct edge {
	uint32_t to;
	COST_T cost;
	edge() {}
	edge(uint32_t to_, COST_T cost_)
		:to(to_), cost(cost_) {}
};
std::vector<edge> graph[N_MAX];

//ダイクストラ
COST_T D_[N_MAX][N_MAX];
void Dijkstra(uint32_t s)
{
	auto&D = D_[s];
	using P = std::pair<COST_T, uint32_t>;//cost pos
	std::priority_queue<P, std::vector<P>, std::greater<>> que;
	std::fill(std::begin(D), std::end(D), INF);
	D[s] = 0;
	que.emplace(0, s);
	while (!que.empty())
	{
		auto p = que.top(); que.pop();
		const auto& nowpos = p.second;
		const auto& nowcost = p.first;
		if (D[nowpos] < nowcost) { continue; }

		//for (int32_t to = 0; to < N; ++to)
		//{
		//	auto cost = nowcost + graph[nowpos][to];
		//	if (cost < D[to]) {
		//		D[to] = cost;
		//		que.emplace(D[to], to);
		//	}
		//}

		for (const auto& e : graph[nowpos])
		{
			auto cost = nowcost + e.cost;
			if (cost < D[e.to]) {
				D[e.to] = cost;
				que.emplace(cost, e.to);
			}
		}

	}
}



int main()
{
	using std::endl;
	in.sync_with_stdio(false);
	out.sync_with_stdio(false);
	in.tie(nullptr);
	out.tie(nullptr);

	int32_t H, W;

	in >> H >> W;
	for (int32_t a = 0; a < 10; a++)
	{
		for (int32_t b = 0; b < 10; b++)
		{
			int c;
			in >> c;
			graph[a].emplace_back(b, c);
		}
	}
	for (int32_t i = 0; i < 10; i++)
	{
		Dijkstra(i);
	}
	int64_t sum = 0;
	for (int32_t i = 0; i < H; i++)
		for (int32_t j = 0; j < W; j++)
		{
			int a;
			in >> a;
			if (a != -1) { sum += D_[a][1]; }
		}
	out << sum << endl;

	return 0;
}
#endif


Measured memory_bytes: 256