#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n, k;
cin >> n >> k;
vector<vector<pair<ll, ll>>> a(k, vector<pair<ll, ll>>(n));
vector<vector<ll>> b(n, vector<ll>(k));
vector<ll> count(n), start(k), pos(k);
queue<ll> q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
cin >> a[j][i].first;
a[j][i].second = i;
if (a[j][i].first == 0) {
count[i]++;
pos[j]++;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
cin >> b[i][j];
}
}
for (int i = 0; i < n; i++) {
if (count[i] == k) {
q.push(i);
}
}
for (int i = 0; i < k; i++) {
sort(a[i].begin(), a[i].end());
}
ll ans = 0;
while (!q.empty()) {
ll idx = q.front();
q.pop();
ans++;
for (int i = 0; i < k; i++) {
start[i] += b[idx][i];
while (pos[i] < n and a[i][pos[i]].first <= start[i]) {
count[a[i][pos[i]].second]++;
if (count[a[i][pos[i]].second] == k) {
q.push(a[i][pos[i]].second);
}
pos[i]++;
}
}
}
cout << ans << "\n";
}