#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef vector vi; typedef pair pii; #define MP make_pair #define PB push_back #define inf 1000000007 #define rep(i,n) for(int i = 0; i < (int)(n); ++i) #define all(x) (x).begin(),(x).end() template void Fill(A (&array)[N], const T &val){ std::fill( (T*)array, (T*)(array+N), val ); } template inline bool chmax(T &a, T b){ if(a inline bool chmin(T &a, T b){ if(a>b){ a = b; return true; } return false; } int main(){ int n,k; cin >> n >> k; vector a(n); rep(i,n) cin >> a[i]; vector b(n); rep(i,n) cin >> b[i]; vector > p(n,vector(n)); priority_queue > > pq; rep(i,n){ rep(j,n){ cin >> p[i][j]; pq.push(MP(p[i][j],MP(i,j))); } } vector > res(n,vector(n)); while(!pq.empty()){ auto x = pq.top(); pq.pop(); int i = x.second.first; int j = x.second.second; if(a[i]>0&&b[j]>0){ a[i]--; b[j]--; res[i][j]++; pq.push(MP(x.first-1,MP(i,j))); } } ll ans =0; rep(i,n){ rep(j,n){ ans += (res[i][j]-p[i][j])*(res[i][j]-p[i][j]); } } cout << ans << endl; return 0; }