#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fi first
#define se second
using namespace std;
mt19937_64 rng(time(NULL));
ll rand(ll l, ll r) {
return rng() % (r - l + 1) + l;
}
ll power(ll a, ll b) {
if (b == 0) return 1;
ll ans = power(a, b / 2);
ans *= ans;
if (b % 2 == 1) ans *= a;
return ans;
}
const ll N = 1e6 + 5;
ll n, k, p[N], cnt[N];
pair<ll, ll> P[N];
vector<vector<ll>> r, u;
void solve() {
cin >> n >> k;
if (k == 1) {
for (int i = 0; i < n; i ++) {
cin >> P[i].fi;
}
for (int i = 0; i < n; i ++) {
cin >> P[i].se;
}
sort(P, P + n);
ll t = 0;
ll ans = 0;
for (int i = 0; i < n; i ++) {
if (t >= P[i].fi) {
ans ++;
t += P[i].se;
}
else break;
}
cout << ans;
return;
}
for (int i = 0; i < k; i ++) p[i] = 0;
for (int i = 0; i < n; i ++) {
r.push_back({});
for (int j = 0; j < k; j ++) {
ll x; cin >> x;
r[i].push_back(x);
}
}
for (int i = 0; i < n; i ++) {
u.push_back({});
for (int j = 0; j < k; j ++) {
ll x; cin >> x;
u[i].push_back(x);
}
}
if (n == 1) {
bool ok = true;
for (int i = 0; i < k; i ++) {
if (r[0][i] != 0) {
ok = false;
break;
}
}
if (ok == true) {cout << 1; return;}
cout << 0; return;
}
ll t = 0;
bool nig = true;
while (nig == true) {
nig = false;
for (int i = 0; i < n; i ++) if (cnt[i] == 0) {
bool ok = true;
for (int j = 0; j < k; j ++) {
if (p[j] < r[i][j]) {
ok = false;
break;
}
}
if (ok == true) {
for (int j = 0; j < k; j ++) {
p[j] += u[i][j];
}
t ++;
cnt[i] ++;
nig = true;
}
}
}
cout << t;
}
int main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
ll query = 1;
// cin >> query;
while (query --) solve();
}