Getting wrong answer on one of the test case for print LCS problem DP

#include
using namespace std;
int main() {
int i,n,m,j;
string inp,pat;
int arr[1001][1001];
cin>>inp;
cin>>pat;
n=inp.length();
m=pat.length();
for(i=0;i<=n;++i){
arr[i][0]=0;
}
for(i=0;i<=m;++i){
arr[0][i]=0;
}
for(i=1;i<=n;++i){
int q=0;
//cout<<endl;
for(j=1;j<=m;++j){
if(inp[i-1]==pat[j-1]){
q=1+arr[i-1][j-1];
}
else
q=max(arr[i-1][j],arr[i][j-1]);
arr[i][j]=q;
}
}
for(i=0;i<=m;++i){
if(arr[n][i]>arr[n][i-1]){
cout<<pat[i-1];
}
}
//cout<<arr[m][n];

return 0;

}

Save your code on ide.codingblocks.com and then share its link.