#if __INCLUDE_LEVEL__ == 0
#include __BASE_FILE__
void Solve() {
constexpr int64_t INF64 = INT64_MAX / 10;
int n, m;
IN(n, m);
vector g(3, vector(n, vector<int64_t>(n, INF64)));
while (m--) {
int a, b, c, d;
IN(a, b, c, d);
--a, --b;
for (int z : Rep1(0, 2)) {
if (z <= d) {
SetMin(g[z][a][b], c);
}
}
}
for (int z : Rep1(0, 2)) {
for (int i : Rep(0, n)) {
g[z][i][i] = 0;
}
for (int k : Rep(0, n)) {
for (int i : Rep(0, n)) {
for (int j : Rep(0, n)) {
SetMin(g[z][i][j], g[z][i][k] + g[z][k][j]);
}
}
}
}
vector h(n, vector<int64_t>(n, INF64));
for (int i : Rep(0, n)) {
for (int j : Rep(0, n)) {
h[i][j] = min(g[2][i][j], 2 * g[1][i][j] + g[0][j][i]);
}
}
for (int k : Rep(0, n)) {
for (int i : Rep(0, n)) {
for (int j : Rep(0, n)) {
SetMin(h[i][j], h[i][k] + h[k][j]);
}
}
}
for (int i : Rep(0, n)) {
for (int j : Rep(0, n)) {
if (h[i][j] >= INF64) {
h[i][j] = -1;
}
}
OUT(h[i]);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
Solve();
}
#elif __INCLUDE_LEVEL__ == 1
#include <bits/stdc++.h>
template <class T> concept Range = std::ranges::range<T> && !std::convertible_to<T, std::string_view>;
template <class T> concept Tuple = std::__is_tuple_like<T>::value && !Range<T>;
namespace std {
istream& operator>>(istream& is, Range auto&& r) {
for (auto&& e : r) is >> e;
return is;
}
istream& operator>>(istream& is, Tuple auto&& t) {
apply([&](auto&... xs) { (is >> ... >> xs); }, t);
return is;
}
ostream& operator<<(ostream& os, Range auto&& r) {
auto sep = "";
for (auto&& e : r) os << exchange(sep, " ") << e;
return os;
}
ostream& operator<<(ostream& os, Tuple auto&& t) {
auto sep = "";
apply([&](auto&... xs) { ((os << exchange(sep, " ") << xs), ...); }, t);
return os;
}
} // namespace std
using namespace std;
#define LAMBDA2(x, y, ...) ([&](auto&& x, auto&& y) -> decltype(auto) { return __VA_ARGS__; })
#define Rep(...) [](int l, int r) { return views::iota(min(l, r), r); }(__VA_ARGS__)
#define Rep1(...) [](int l, int r) { return Rep(l, r + 1); }(__VA_ARGS__)
#define SetMin(...) LAMBDA2(x, y, y < x && (x = y, 1))(__VA_ARGS__)
#define INF64 (INT64_MAX / 2)
#define IN(...) (cin >> forward_as_tuple(__VA_ARGS__))
#define OUT(...) (cout << forward_as_tuple(__VA_ARGS__) << '\n')
#endif // __INCLUDE_LEVEL__ == 1