#include using namespace std; #define rep(i,n) for(int i = 0; i < (n);i++) #define sz(x) int(x.size()) typedef long long ll; typedef pair P; vector> a; int n, m, k; /* int dfs(int i, int now){ if (now > k) return -1; if (i == n) return now; int ret = -1; for (auto e : a[i]) { int t = dfs(i+1,now + e); if (t == -1) break; ret = max(ret,t); } return ret; } */ int main(){ cin >> n >> m >> k; a.resize(n); int value = 0; rep(i,n) rep(j,m) { int c; cin >> c; if (j == 0) value += c; a[i].push_back(c); } if (value > k) cout << -1 << endl; else { for (int i = 1; i < m; i++) { for (int j = 0; j < n; j++) { if (value - a[j][i-1] + a[j][i] <= k) value += a[j][i] - a[j][i-1]; else break; } } cout << k - value << endl; } return 0; }