#include<map>

#include<set>

#include<cmath>

#include<stack>

#include<queue>

#include<string>

#include<cstdio>

#include<vector>

#include<cctype>

#include<cassert>

#include<utility>

#include<numeric>

#include<cstring>

#include<iostream>

#include<algorithm>

using namespace std;

#define pr pair

#define PR pair<int,int>

#define MP make_pair

#define SI(x) set<x>::iterator

#define VI(x) vector<x>::iterator

#define MI(x,y) map<x,y>::iterator

#define SRI(x) set<x>::reverse_iterator

#define VRI(x) vector<x>::reverse_iterator

#define MRI(x,y) map<x,y>::reverse_iterator

#define F first

#define S second

#define Sz(x) (int)x.size()

#define clrQ(x) while(!x.empty)x.pop();

#define clr(x,y) memset(x,y,sizeof(x));

#if defined (_WIN32) || defined (__WIN32) || defined (WIN32) || defined (__WIN32__)

#define LL __int64

#define LLS "%" "I" "6" "4" "d"

#define LLU "%" "I" "6" "4" "u"

#define LL_MAX _I64_MAX

#else

#define LL long long

#define LLS "%" "l" "l" "d"

#define LLU "%" "l" "l" "u"

#define LL_MAX _I64_MAX

#endif

const int inf = ~0u >> 1;

const LL lnf = ~0ull >> 1;

#define MOD 6

int n, k;

struct Matrix {

int n, m;

int** M;

Matrix(int n, int m) :n(n), m(m) {

M = new int*[n];

for (int i = 0; i < n; i++)

M[i] = new int[m];

}

Matrix(int n, int m, int k) :

n(n), m(m) {

M = new int*[n];

for (int i = 0; i < n; i++)

M[i] = new int[m];

Init(k);

}

~Matrix() {

if (M) {

for (int i = 0; i < n; i++) {

if (M[i]) {

delete[] M[i];

M[i] = NULL;

}

}

delete[] M;

M = NULL;

}

}

void Init(bool k) {

for (int i = 0; i < n; i++)

for (int j = 0; j < m; j++)

M[i][j] = k * (i == j);

}

void out() {

for (int i = 0; i < n; i++)

for (int j = 0; j < m; j++)

printf("%d%c", M[i][j], j == m - 1 ? '\n' : ' ');

}

Matrix & operator=(const Matrix& othr) {

this->~Matrix();

n = othr.n;

m = othr.m;

M = new int*[n];

for (int i = 0; i < n; i++)

M[i] = new int[m];

for (int i = 0; i < n; i++) {

for (int j = 0; j < m; j++) {

M[i][j] = othr.M[i][j];

}

}

return *this;

}

Matrix(const Matrix & othr) {

n = othr.n;

m = othr.m;

M = new int*[n];

for (int i = 0; i < n; i++)

M[i] = new int[m];

for (int i = 0; i < n; i++) {

for (int j = 0; j < m; j++) {

M[i][j] = othr.M[i][j];

}

}

}

bool operator==(const Matrix& othr) const {

if (n - othr.n || m - othr.m) return false;

for (int i = 0; i < n; i++) {

for (int j = 0; j < m; j++) {

if (M[i][j] != othr.M[i][j]) return false;

}

}

return true;

}

Matrix operator *(const Matrix& othr) const {

if (m - othr.n) exit(1);

Matrix ans(n, othr.m, 0);

for (int i = 0; i < n; i++)

for (int j = 0; j < othr.m; j++)

for (int k = 0; k < m; k++) {

ans.M[i][j] += M[i][k] * othr.M[k][j] % MOD;

if (ans.M[i][j] >= MOD) ans.M[i][j] -= MOD;

if (ans.M[i][j] < 0) ans.M[i][j] += MOD;

}

return ans;

}

Matrix operator *(const int& x) const {

Matrix ans(n, m);

for (int i = 0; i < n; i++)

for (int j = 0; j < m; j++)

ans.M[i][j] = M[i][j] * x % MOD;

return ans;

}

Matrix operator +(const Matrix & othr) const {

if (n - othr.n || m - othr.m) exit(1);

Matrix ans(n, m);

for (int i = 0; i < n; i++)

for (int j = 0; j < m; j++) {

ans.M[i][j] = M[i][j] + othr.M[i][j];

if (ans.M[i][j] >= MOD) ans.M[i][j] -= MOD;

if (ans.M[i][j] < 0) ans.M[i][j] += MOD;

}

return ans;

}

Matrix operator -(const Matrix& othr) const {

if (n - othr.n || m - othr.m) exit(1);

Matrix ans(n, m);

for (int i = 0; i < n; i++)

for (int j = 0; j < m; j++) {

ans.M[i][j] = M[i][j] - othr.M[i][j];

if (ans.M[i][j] >= MOD) ans.M[i][j] -= MOD;

if (ans.M[i][j] < 0) ans.M[i][j] += MOD;

}

return ans;

}

Matrix operator ^(int x) const {

if (n - m) exit(1);

Matrix ans(n, m, 1), base = *this;

while (x > 0) {

if (x & 1) ans = ans * base;

base = base * base;

x >>= 1;

}

return ans;

}

};

int main(int argc, char **argv) {

while (~scanf("%d%d", &n, &k)) {

if (n == 0 && k == 0) break;

Matrix A(n, k, 0), B(k, n, 0);

for (int i = 0; i < n; i++) {

for (int j = 0; j < k; j++) {

scanf("%d", &A.M[i][j]);

}

}

for (int i = 0; i < k; i++) {

for (int j = 0; j < n; j++) {

scanf("%d", &B.M[i][j]);

}

}

Matrix mat = B * A;

int r = n * n - 1;

Matrix res = A * (mat ^ r) * B;

int ans = 0;

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

ans += res.M[i][j];

}

}

printf("%d\n", ans);

}

}