#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf=10000000000;
const ll N=1000,M=1e6;
ll n,m,S,T,h[N],ne[M],e[M],f[M],d[M],cur[M],idx;
void add(ll u,ll v,ll w){
e[idx]=v,ne[idx]=h[u],f[idx]=w,h[u]=idx++;
e[idx]=u,ne[idx]=h[v],f[idx]=0,h[v]=idx++;
}
bool bfs(){
memset(d,-1,sizeof d);
d[S]=0;
cur[S]=h[S];
queue <ll> q;
q.push(S);
while(q.size()){
ll t=q.front();q.pop();
for(int i=h[t];i!=-1;i=ne[i]){
int ver=e[i];
if(d[ver]==-1 && f[i]){
d[ver]=d[t]+1;
cur[ver]=h[ver];
if(ver==T) return true;
q.push(ver);
}
}
}
return false;
}
ll find(ll u,ll limit){
if(u==T) return limit;
ll flow=0;
for(int i=cur[u];i!=-1 && flow<limit;i=ne[i]){
cur[u]=i;
int ver=e[i];
if(d[ver]==d[u]+1 && f[i]){
ll t=find(ver,min(f[i],limit-flow));
if(t==0) d[ver]=-1;
f[i]-=t;f[i^1]+=t;flow+=t;
}
}
return flow;
}
ll dinic(){
ll ans=0,flow;
while(bfs()) while(flow=find(S,inf)) ans+=flow;
return ans;
}
int main(){
cin>>n>>m;
memset(h,-1,sizeof h);
S=0,T=n+m+1;
ll sum=0;
for(ll i=1;i<=n;i++){
ll x;
cin>>x;
sum+=x;
add(S,i,x);
}
for(ll i=1;i<=m;i++){
ll x;
cin>>x;
add(i+n,T,x);
}
for(ll i=1;i<=n;i++){
for(ll j=1;j<=m;j++){
add(i,n+j,1);
}
}
ll final=dinic();
vector <vector <ll>> ans(n+1);
if(sum==final){
cout<<1<<endl;
for(int i=0;i<idx;i+=2){
if(e[i]>n && e[i^1]<=n && !f[i]){
ans[e[i^1]].push_back(e[i]-n);
}
}
for(ll i=1;i<=n;i++){
for(auto t:ans[i]){
cout<<t<<' ';
}
cout<<endl;
}
}
else cout<<0<<endl;
return 0;
}