#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 10;
struct Matrix {
int row, col;
int matrix[maxn][maxn];
Matrix() {};
Matrix(int r, int c) : row(r), col(c) {}
};
Matrix Add(Matrix x, Matrix y) {
Matrix answer = Matrix(x.row, x.col);
for (int i = 0; i < answer.row; i++) {
for (int j = 0; j < answer.col; j++) {
answer.matrix[i][j] = x.matrix[i][j] + y.matrix[i][j];
}
}
return answer;
}
int main() {
int m, n;
while (scanf("%d%d", &m, &n) != EOF) {
if (m == 0) {
break;
}
int res=0;
Matrix x(m, n);
Matrix y(m, n);
for (int i = 0; i < x.row; i++) {
for (int j = 0; j < x.col; j++) {
scanf("%d", &x.matrix[i][j]);
}
}
for (int i = 0; i < y.row; i++) {
for (int j = 0; j < y.col; j++) {
scanf("%d", &y.matrix[i][j]);
}
}
Matrix answer = Add(x, y);
bool tag = true;
for (int i = 0; i < x.row; i++) { //行固定,遍历列是否为0
for (int j = 0; j < x.col; j++) {
if (answer.matrix[i][j] != 0) {//ij,第一个变量i不变,row不变
tag = false;
break;
}
}
if (tag == true) {
res++;
}
tag = true; //恢复标志位
}
for (int i = 0; i < x.col; i++) { //列固定,遍历行是否为0
for (int j = 0; j < x.row; j++) {
if (answer.matrix[j][i] != 0) {//ji,第一个变量i不变,col不变
tag = false;
break;
}
}
if (tag == true) {
res++;
}
tag=true;
}
printf("%d\n", res);
}
return 0;
}